CONTENTS | PREV | NEXT | INDEX J2EE BluePrints



10.10 Transactions

The sample application's persistent data is stored in two databases: eStoreDataSource and InventoryDataSource. The eStoreDataSource database holds information about accounts and orders. The InventoryDataSource database holds information about products, product categories, and the inventory of each product. When an order is placed, ShoppingClientController must access both databases. The sample application uses J2EE SDK support for distributed transactions to reduce the inventory of ordered products and add a new entry to the order table in an atomic operation. Note that a J2EE product is not required to support access to multiple JDBC databases within a single transaction. However, some J2EE products might choose to provide these extra transactional capabilities.

Recall that ShoppingClientController delegates the implementation of order processing to a helper class named StateMachine. StateMachine is responsible for maintaining consistency among the database tables represented by the enterprise beans that it calls. When StateMachine handles an order event it invokes the method createOrder illustrated in Code Example 10.17. For each bean, StateMachine gets a reference to the home interface and then creates an instance of the bean. StateMachine loops through the list of items in the order, finds the appropriate inventory item, and updates the inventory table for that item. Simply creating an instance of the Order bean causes an entry to be added to the order, lineitem, and orderstatus tables.

Note that neither the StateMachine.createOrder nor individual bean operations explicitly invoke transactions, because ShoppingClientController uses container-managed transactions. As a result, all database operations invoked by ShoppingClientController are automatically wrapped in a transaction by the container. The transaction context is automatically propagated to any enterprise beans that ShoppingClientController invokes (in this case Inventory and Order).



CONTENTS | PREV | NEXT | INDEX
Copyright © 2001 Sun Microsystems, Inc. All Rights Reserved.