1 Reply Latest reply on Feb 28, 2003 2:47 PM by Lars Bergmann

    Concurrent access to and synchronization on entity.create()

    Lars Bergmann Newbie

      Hi all,

      we have a situation of excessively concurrent entity instance creation.
      Each create-method runs in a dedicated transaction. In case of parallel
      request of creation we get the correct transactional behaviour:

      1. findByPrimaryKey() fails for the second concurrent request (since the
      first tx with create has not yet been completed).

      2. Two different physical entity instances with identical prinmary keys are created.

      3. The second transaction fails (DuplicateKeyException).

      We have the requirement of returning the first valid instance to both requests
      instead of exception handling for the second failed tx. What is the most effective
      way of synchronizing the create-method of an entity instance? Is there any internal
      mechanism to synchronize concurrent create-methods on the primary key (JBoss 2.4.10)?



        • 1. Re: Concurrent access to and synchronization on entity.creat
          Lars Bergmann Newbie

          I have some more details on this
          (the last number in a row is system time in millisecs):

          [INFO] ProductManager findByPrimaryKey: [.113.2213167.2211399.] - 5981330 - 1046460849825
          [INFO] ProductManager findByPrimaryKey: [.113.2213167.2211399.] - 6992616 - 1046460849835
          [INFO] Product ejbCreate START: [.113.2213167.2211399.] - 1888960 - 1046460849885
          [INFO] Product ejbCreate END: [.113.2213167.2211399.] - 1888960 - 1046460849895
          [INFO] ProductManager: New product instance created: [.113.2213167.2211399.]
          [INFO] Product ejbCreate START: [.113.2213167.2211399.] - 3602759 - 1046460849965
          [INFO] Product ejbCreate END: [.113.2213167.2211399.] - 3602759 - 1046460849965
          javax.transaction.TransactionRolledbackException: Could not create product instance: javax.ejb.DuplicateKeyException: En
          tity with key [.113.2213167.2211399.] already exists; nested exception is:

          Obviously, findByPrimaryKey() is called by two different session bean instances (ProductManager);
          and the first create() is started after this sequence. The second create() is called after the
          first has finished since session instance #2 still assumes non-existence of this product.

          Sooo..., is there an efficient (off-spec ) jboss-internal hack, that enables the second (or any subsequent) call to ejbCreate() to return the valid instance instead of throwing an exception?

          Any ideas welcome,