1 Reply Latest reply on Jul 20, 2006 11:06 AM by doug.palmer

    How do I lock an Entity bean?

    doug.palmer

      Hi

      I am running EJB3 with JBoss 4.0.4 and I am trying to lock an Entity bean while I am accessing/updating it. However, I don't seem to be able to lock the bean. My system consists of:

      A JMS queue; a MDB listening to the queue and an Entity bean for persistence.

      The client places an update for an instance of the entity bean on the JMS queue. A MDB removes the data; checks that the relevant instance of the entity bean is available; performs some processing on the data and persists the data in the entity bean.

      The client may send multiple updates for the same instance of the entity bean but the processing must occur in isolation. So the MDB needs to lock the Entity while it is processing and release it when it has persisted the data.

      I have tried various combinations of the following but multiple MDB instances manage to access the same Entity instance:

      1. Annotate MDB.onMessage() with @TransactionAttribute(TransactionAttributeType.REQUIRED).

      2. Set the type of the EntityManagers @PersistentContext to PersistenceContextType.TRANSACTION.

      3. Adding @ActivationConfigProperty(propertyName = "Transacted", propertyValue = "true"), @ActivationConfigProperty(propertyName = "Xa", propertyValue = "true") to the MDB's activation config.

      4. Calling EntityManager.lock(Entity, LockModeType.READ).


      Can anyone tell me how to pessimistically lock the Entity instance? What have I missed?

      Regards
      Doug

        • 1. I have found a workaround
          doug.palmer

          I have found a workaround but it still doesn't give me the locking semantics that I would like. I have added a version number to my entity bean and added the @Version annotation.

          This means that the MDBs can read dirty data but can't write after using it; this stops the symptoms but doesn't fix the cause. Does anyone know how I can completely lock an Entity within a transaction?

          Regards
          Doug