I
Ittay Freiman
The recipe to my problem:
1. an entity bean, with an Integer porperty 'foo'.
2. the entity bean also has a finder method for finding beans whose
'foo' property is null.
3. a session bean with a method m()
In method m(), the finder method is called, and the first bean is
selected and its foo property gets a value (say 1, so it is no longer
null).
m() needs to be synchronized, so no two threads will get the same bean
in the finder call. so i've created a static object and call
'synchronized(obj)' before doing the work.
However, when two threads call m(), one of them gets a collection,
selects the first bean, changes its value. Then, before that thread
fully returns to the caller (the caller uses a local interface), the
second thread enters m(), and gets the same collection, with the same
bean as the first, and it has 'foo' as null.
When I look in the DB, I can see that the first thread indeed changed
the property.
When I synchronize the callers (I add a 'synchronized()' call before
calling the local interface) everything works ok.
I guess the problem is that until the method returns to the caller,
the transaction is not fully complete, and so the server doesn't
update its internal structures.
How can I make this work without having to synchronize every call to
m()?
Thank you,
Ittay
1. an entity bean, with an Integer porperty 'foo'.
2. the entity bean also has a finder method for finding beans whose
'foo' property is null.
3. a session bean with a method m()
In method m(), the finder method is called, and the first bean is
selected and its foo property gets a value (say 1, so it is no longer
null).
m() needs to be synchronized, so no two threads will get the same bean
in the finder call. so i've created a static object and call
'synchronized(obj)' before doing the work.
However, when two threads call m(), one of them gets a collection,
selects the first bean, changes its value. Then, before that thread
fully returns to the caller (the caller uses a local interface), the
second thread enters m(), and gets the same collection, with the same
bean as the first, and it has 'foo' as null.
When I look in the DB, I can see that the first thread indeed changed
the property.
When I synchronize the callers (I add a 'synchronized()' call before
calling the local interface) everything works ok.
I guess the problem is that until the method returns to the caller,
the transaction is not fully complete, and so the server doesn't
update its internal structures.
How can I make this work without having to synchronize every call to
m()?
Thank you,
Ittay