users@glassfish.java.net

concurrently modifying entity instance

From: <glassfish_at_javadesktop.org>
Date: Wed, 16 Apr 2008 23:00:05 PDT

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:

---------------------- code begins ----------------------

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

In producer's session:

---------------------- code begins ----------------------

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


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
[Message sent by forum member 'gsegura' (gsegura)]

http://forums.java.net/jive/thread.jspa?messageID=269700