0 Replies Latest reply on Apr 15, 2009 4:22 PM by gianfranco zas

    Concurrent modifications of the same attribute over JPA entities at diferent conversations.

    gianfranco zas Newbie
      Concurrent modifications of the same attribute over JPA entities at diferent conversations.

      Hi.

      I have a problem related to concurrent modifications.

      These area my thin jpa entities:

      @Entity
      class A {
       
        private Long id;
        private List<B> bList;
       
        @Id
        @GeneratedValue
        public Long getId() {
          return id;
        }
       
        // other properties aren't specified
        @OneToMany(cascade = CascadeType.ALL)
        public List<B> getBList() {
          return bList;
        }
      }


      @Entity
      public class B {

        private Long id;
       
        @Id
        @GeneratedValue
        public Long getId() {
          return id;
        }
       
        // other properties aren't specified
       
        // It hasn't got a reference to A
      }


      We have (for example) two conversations, each of them invoking a method named x1

      @Name("AnotherManager")
      @Scope(ScopeType.CONVERSATION)
      public class SystemHelper {

        @In(create = true)
        EntityManager entityManager;

        public void x1() {
          b = new B();
         
          A a = entityManager.find(A.class, 1L);
          a.getBList().add(b)
        }
      }

      OK, the two invocations to 'x1' occurs synchronously (first at c1, and after completion 'x1()' occurs at c2), but when c2 completes, the association between 'a' whose id=1 and the 'b1' created at 'c1' is lost. 'b1' continues persisted in the database table, but the association at the join table is lost, only the second association is maintained. I repeat, the two invocations occurs synchronously but the conversations c1 and c2 occurs simultaneously and because we have two different conversations, the persistent contexts are diferent each other.

      We know that this behavior is caused by the modification of the same attribute (bList) and we want to explore the posible solutions.

      So the question is: what are the ideal strategies to the problem of modifying concurrently the same attribute at diferent JPA entity references having the same database id

      Best regards