4 Replies Latest reply on Feb 17, 2008 2:07 PM by Pete Muir

    serializing access to resource

    Gerardo Segura Newbie

      I need to access an entity bean instance in a serial fashion, so I kindly request for any pattern to implement this.

      The use case is similar to managing an account balance, the account must be blocked during modification. I need to block other requests until the resource is available (as if it was a printer). That is why optimistic locking is not suitable.

      I don't think synchronizing the SFSB which handles the entity bean is the best way since that would serialize invocation regardless of the entity bean instance been updated, I only want to avoid concurrently modifying the same entity bean instance.

      Jboss 4.0 official guide describes the transaction lock behaviour which:

      ensures that only one transaction at a time has access to a given entity bean

      But it is not clear to me how this apply in the case, for example, of two entitymanagers loading same entity bean. Does it mean the second one's thread will be locked at that precise moment?

      Also I don't quite grasp the behavior of entitymanager.lock(), what happens when another thread tries to lock one entity bean instance already locked? the locking effect is global to different entitymanager instances?

      I deeply appreciate any advice

        • 1. Re: serializing access to resource
          Norman Richards Master

          I think I'd try pessimistic (db) locking. I'd suggest asking in the Hibernate forums to find out the best way to get the type of locking you want.

          • 2. Re: serializing access to resource
            Gerardo Segura Newbie

            The problem is I don't use hibernate directly but through JPA, besides the hibernate's documentation says:

            Hibernate will always use the locking mechanism of the database, never lock objects in memory!

            But can't find description of behaviour when some thread tries to access an already locked row. It willl be blocked? an exception thrown?

            Do you think is it possible to achieve this at the object level? by means of locking the entity bean itself?

            It would be great to do this via some stablished and robust api, the previously cited documentation (Jboss 4.0 official guide) mentions something about it, but not in a useful hands-on way (at least to my taste).

            Otherwise, is the only option left to implement this ?

            best regards

            • 3. Re: serializing access to resource
              Gerardo Segura Newbie

              Ok, perhaps the correct question is:

              Does EJB3 persistence honors this aspects of Entity Bean Life Cycle (these behaviour is described in http://www.ubookcase.com/book/Sams/JBoss.4.0.The.Official.Guide/0672326485/ch05lev1sec4.html):

              With the default configuration of JBoss there is only one active instance of a given entity bean in memory at one time

              and the locking policy defined in each container configuration in standardjboss.xml:


              I think if this two pieces are honored, then all I have to assure is the proper container-configuration (Standard CMP 2.x EntityBean) is in effect on my entity bean, what do you think??

              please keep in mind by any means I'm not suggesting any disrespect via disinformation. I'm just kindly asking for some insight about this pessimistic locking issue, which at some point one self have to face.


              PS: The reason I formulate the question as that is because the book Pro EJB3 JPA says the JPA doesn't support a portable way to do pessimistic locking.