3 Replies Latest reply on Nov 25, 2005 4:42 AM by Emmanuel Bernard

    Concurrency in Transactions

    Daniel González Newbie

      Hello.

      I'm using Jboss4.0.3SP1 with MySQL 5. I'm newbie of course.

      I'm trying some concepts about concurrency in transactions. I've read that in EJB3 I should not be worried about this concepts. I have a table STUDENT with a "Name" field. I have a Stateless Session Bean with a method (I supose it's transactional (In fact, rollbacks when an exception is thrown works) ) that doesn't behaves as an atomic method:

      public @Stateless class StudentsManagerBean implements StudentsManager {

      @PersistenceContext
      private EntityManager manager;

      //@TransactionAttribute(TransactionAttributeType.REQUIRED)
      public void modifyStudent(){
      //manager.getTransaction().begin();

      Student toret = manager.find(Student.class, "a");
      toret.setName(toret.getNombre()+"_modified");

      try {
      System.out.println("Other request may arrive!!");
      Thread.sleep(10000);
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

      }

      manager.flush();
      //manager.getTransaction().commit();
      }


      When I try to call this method from 2 clients in the follow manner:
      Client 1 calls modifyStudent()
      Client 1 gets the Student "a" from the database
      Client 1 modifies the field of the student
      Client 1 sleeps....
      Client 2 calls modifyStudent()
      Client 2 gets the Student "a" from the database
      Client 2 modifies the field of the student
      Client 2 sleeps....
      Client 1 finishes the transaction
      Client 2 finishes the transaction

      If the method is really atomic, the field "name" in the database must be "studentname_modified_modified" (with two append opperation), but I get "studentname_modified". This behaviour is a lost update. I'm thinking in a bank application with a method like "increaseMyMoneyIn1000", calling it twice I get only 1000 more!!

      I'm sure there is a simple solution. Any ideas?. I've trying with multiple variants (EXTENDED EntityManager, merge-flush, flush-merge) !!!!