4 Replies Latest reply on Apr 17, 2008 12:10 PM by Gerardo Segura

    concurrently modifying entity instance

    Gerardo Segura Newbie

      Dear all,
      I'm requesting in the kindest way any advise on this issue: concurrent modification of one entity instance.

      The following is a simplified scenario, but describes the issue well.
      There is this entity which holds a number, one user is in charge of decreasing that magnitude and another one is in charge of incrementing it.

      These two are the methods each user is supposed to invoke during interaction with the application.

      In consumer's session:

      public void decrement() {
       Resource r = manager.find(Resource.class, id);
       while(r.getCounter()>0)
       r.decreaseByOne() ;
      }


      In producer's session:

      public void increment() {
       Resource r = manager.find(Resource.class, id);
       r.increaseCounterBy(100) ;
      }



      Those methods could potentially be invoked simultaneously, so one thing I'd like to prevent is method increment() been called during execution of decrement(), hence I need to either:
      1.- serialize those invocations, or
      2.- pessimistically lock the entity instance.

      For option 2, it is not clear to me how to lock the entity instance 'globally', since each user has his own persistence context entitymanager.lock() won't be enough, right?

      For option 1 I don't even have a clue.


      best regards,
      Gerardo