As you saw in Chapter 12, session beans can interact directly with the
database as easily as they can manage the workflow of other
enterprise beans. The ProcessPayment EJB, for example, makes inserts
PAYMENT table when the
byCredit() method is invoked, and the TravelAgent
EJB queries the database directly when the
listAvailableCabins() method is invoked. Stateless
session beans such as the ProcessPayment EJB have no conversational
state, so each method invocation must make changes to the database
immediately. With stateful session beans, however, we may not want to
make changes to the database until the transaction is complete.
Remember, a stateful session bean can be just one participant out of
many in a transaction, so it may be advisable to postpone database
updates until the entire transaction is committed or to avoid updates
if it is
There are several different scenarios in which a stateful session
bean would want to cache changes before applying them to the
database. For example, think of a shopping cart implemented by a
stateful session bean that accumulates several items for purchase. If
the stateful bean implements
SessionSynchronization, it can cache the items and
write them to the database only when the transaction is complete.
javax.ejb.SessionSynchronization interface allows a session bean to receive additional notification of the session’s involvement in transactions. The addition of these transaction ...