This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.