O'Reilly logo

Enterprise JavaBeans, Second Edition by Richard Monson-Haefel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Transactional Stateful Session Beans

As you saw in Chapter 7, session beans can interact directly with the database as easily as they can manage the workflow of other beans. The ProcessPayment bean, for example, makes inserts into the PAYMENT table when the byCredit() method is invoked. The TravelAgent bean queries the database directly when the listAvailableCabins() method is invoked. With stateless session beans like ProcessPayment, there is 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’s rolled back.

There are several different scenarios in which a stateful session bean would want to cache changes before applying them to the database. In Chapter 9, we will take a look at modeling entity business concepts in stateful session beans that implement the SessionSynchronization interface. These sessions may have their methods invoked many times before writing to the database. For example, think of a shopping cart implemented by a stateful session bean that accumulates several items for purchase. If the bean implements SessionSynchronization, it can cache the items and only write them to the database when ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required