Java Reference
In-Depth Information
Concurrent access to a singleton EJB is, by default, controlled by the container.
Read and write access to a singleton is limited to one client at a time. However, it
is possible to provide a finer level of concurrency control through the use of annota-
tions. This can be achieved using the @Lock annotation whose arguments determ-
ine the type of concurrency access permitted.
By using an @Lock annotation of type javax.ejb.LockType.READ , multithreaded
access will be allowed to the bean.
@Lock(READ)
public ArrayList<Seat> getSeatList() {
return seatList;
}
On the other hand, if we apply javax.ejb.LockType.WRITE , the single-threaded
access policy is enforced.
@Lock(WRITE)
public void buyTicket(Seat seat) {
seat.setBooked(true);
}
The general idea is to use READ type locks on methods that just read values from the
cache and WRITE type locks for methods that change the values of elements con-
tained in the cache.
Using bean-managed concurrency
The other possible option is using a bean-managed concurrency that can be pursued
by applying the @javax.ejb.ConcurrencyManagement annotation with an argu-
ment of ConcurrencyManagementType.BEAN . This annotation will disable the ef-
fect of the @Lock annotation we have used so far, putting the responsibility of ensur-
ing that the singleton cache does not get corrupted on the developer.
So, in order to ensure that our bookings are preserved, we will need to use a well-
known synchronized keyword on top of the buyTicket method:
Search WWH ::




Custom Search