GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes ReadLock/WriteLock from within EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ReadLock/WriteLock from within EJB" Watch "ReadLock/WriteLock from within EJB" New topic
Author

ReadLock/WriteLock from within EJB

Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104
Hello to all,

My question is this:

I want to synchronize read/write access to the following unit of work

1) read from a database table
2) write to a database table
3) upload and save a file to the file system

Previously in another project I created a class which has a HashMap whose
access is controlled with ReentrantReadWriteLock.ReadLock and WriteLock. This class
was called from within a servlet. Now I have a new task where I have to call a
class from a stateless session EJB. Is it safe to have an EJB and ReadLock/WriteLock
in the same call stack? If not, can you give any pointers into this synchronization
dilemma? Everything I think of involves threads and I've heard that is not safe to
introduce them from an EJB.

Thanks for any help you can give me.



Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

What is not advisable to do from within an EJB is creating new threads. However, if you wish to protect access to a shared resource, you have the complete right to do that
IN fact if you think about it, Database also has locks which are called from an EJB, so i think it would not be wrong to do so from within an EJB.
Having said that the hazards related to locking will apply here as for any other code using locks. These hazards include deadlocks.


apigee, a better way to API!
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104
Hello,

Thank you. I will heed your words.

Ravi
Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
Actually, I think you may have problems, especially if using cluster. Because each node will synchronize the operation on a different HashMap object.

So, as EJB does not implement isolation of transactions, you must use the EIS(database) for this.

I think a flag in the database, indicating that the operation is in progress, is the recommended solution.
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104

Thank you. That is a good solution as we move forward. Better to use that in case clustering will be used later on.

 
GeeCON Prague 2014
 
subject: ReadLock/WriteLock from within EJB