Transactional Stateful Session Beans
Session beans can interact directly with the database as easily as they can manage the taskflow of other enterprise beans. Stateless session beans 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 one of many participants in a transaction, so it might be advisable to postpone database updates until the entire transaction is committed or to avoid updates if it is rolled back.
There are several different scenarios in which a stateful session
bean might 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.
The javax.ejb.SessionSynchronization
interface
allows a session bean to receive additional notification of the session’s
involvement in transactions. The addition of these transaction callback
methods by the SessionSynchronization
interface expands the EJB’s awareness of its lifecycle to include a new
state, the Transactional Method-Ready state. This
is always a part of the lifecycle of a transactional stateful session
bean. Implementing the SessionSynchronization
interface ...
Get Enterprise JavaBeans 3.1, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.