2 Replies Latest reply on Dec 6, 2011 8:07 AM by braca

    Locking, Transaction, SFSB, 2 apps

    braca

      Hello,

       

      I get a row locking error from the database when I try to read (and possibly write) a record from a table which has exactly one row. This table holds a value for generating a key. The problem is that this table/entity is accessed by different applications. It's implement like this:

       

      @Stateful

      @Remote( VZBusinessRemote.class )

      @Local( VZBusinessLocal.class )

      public class VZBean implements VZBusinessLocal, VZBusinessRemote {

       

          @PersistenceContext(unitName="AUnitEJB")

          private EntityManager em;

       

          @EJB( name="vzBeanAVPBasis", beanName="AVPBasisBean" )

          private AVPBasisBusinessLocal basisBean;

       

          private Long key;

       

      @Override

          public void unmarshalAVZ( AVZ avz, int startAt, int endat ){

      ...domanythings...

               key = basisBean.findKeyAndSaveKey();

      ....domanythings...

      }

       

      After invoking the SFSB unmarshalAVZ() and calling basisBean.findKeyAndSaveKey() inside this method other applications cannot read nor write to the table/entity with the key table. Even if I call em.clear() after getting and updating the key table the record is locked for other application, but it should be possible for other apps even to write to the locked record. When the method unmarshalAVZ() is finished, locking isn't a problem anymore, but this method can take a very long time and thus locks the key table.

       

      So - what can I do or what's wrong?

       

      Thanks for an answer

        • 1. Re: Locking, Transaction, SFSB, 2 apps
          wdfink

          As long you use a DB inside the transaction and lock the table (row) you will have such behaviour.

          The application(s) are not very scalable.

           

          You should decouple the key generation and use a different for each app.

          Or you might cache a couple of keys in each application outside this transactions (i.e. use a new transaction to refill the internal cache)

          Drawback is that you might loose some keys if the server goes down or crash.

          • 2. Re: Locking, Transaction, SFSB, 2 apps
            braca

            Thanks for the hint. I solved the problem  - as you suggested - by creating a new transaction when generating the key.

             

            @TransactionAttribute( TransactionAttributeType.REQUIRES_NEW )

                public Long findKeyAndSaveKey(){

            }

             

            That's ok because this method is called only once per session and further keys are generated using an algorithm.