synchronization in session bean

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,441
Members
47,125
Latest member
MDBT

Latest Threads

Top