C H A P T E R 6 |
Transactions |
This chapter covers the use of EJB module property sheets to program container-managed transactions. For bean-managed transactions, see Building Enterprise JavaBeans Components.
Transaction boundaries are determined by the Transaction Attribute properties of the enterprise beans involved in the transaction.
When you create an enterprise bean with the IDE's EJB wizards and select container-managed transactions, the wizard creates the enterprise bean with the default value for the transaction attribute property. This section shows you how to view the default transaction attribute settings and interpret them.
To open the Transaction Settings property editor and review the default settings:
Right-click the EJB module node and choose Properties
Transaction Settings property
ellipsis (...) button.
The Transaction Setting property editor opens.
FIGURE 6-1 shows the Transaction Settings Property Editor for the CatalogData EJB module that was covered in Chapter 3. The values displayed in FIGURE 6-1 are the default transaction attribute settings.
The default transaction attribute settings are displayed in the EJB or Method - Trans-attribute field. To understand this display, notice the following features of the display:
Required is one of the transaction values defined in the Java 2 Platform, Enterprise Edition Specification. The rules for methods that have the Required transaction attribute are listed below:
This is the default behavior for enterprise bean methods.
A business transaction in an EJB module often spans several enterprise beans. A common architecture for enterprise bean business logic is one session bean and several entity beans. Clients call the session bean, and then the session bean calls methods of the entity beans.
For example, the client has data for two new related database records. The session bean generates database inserts by calling create methods of the two entity beans. The two database inserts must be in the same transaction. FIGURE 6-2 shows a transaction of this type.
When you assemble these entity beans into an EJB module, you want the application server to recognize the boundaries of the business transaction. You want the application server to include all of the work that the EJB module performs after a client call in a single database transaction. You want the application server to do the following:
If the application server recognizes these transaction boundaries, all of the work performed by the EJB module will be committed or rolled back together.
To configure transaction boundaries, you open the Transaction Settings property editor and modify the transaction attributes of the enterprise beans involved in the transaction.
The procedure that follows demonstrates editing the transaction attribute settings. This procedure changes the default transaction settings that are shown in FIGURE 6-1 to transaction attribute settings that produce the transaction boundaries shown in FIGURE 6-2.
To modify transaction attributes:
1. Right-click the EJB module node and choose Properties Transaction Settings property
ellipsis (...) button.
The Transaction Settings property editor opens.
2. Change the transaction attributes of the session bean methods to RequiresNew.
You want to change the behavior of the CatalogManagerBean methods so that a call to either of its business methods (getAllItems and getOneItemDetail) starts a new transaction. Changing the transaction attribute to RequiresNew accomplishes this.
a. Click a session bean method to select it.
b. Change the value of the Trans-attribute field to RequiresNew.
Notice that the new transaction attribute value appears after the method node.
3. Change the transaction attributes of the entity bean methods to Mandatory.
You want to change the behavior of the entity bean methods so that these methods execute within the boundaries of the transactions opened by the session bean methods. Changing the transaction attribute to Mandatory accomplishes this. It tells the application server that these methods can only be executed if a transaction is already in progress.
a. Click an entity bean method to select it.
b. Change the value of the Trans-attribute field to Mandatory.
Notice that the new transaction attribute value appears after the method node.
4. Click OK to close the editor and save your changes.
FIGURE 6-3 shows the Transaction Settings Property Editor with the new transaction attributes settings.
The transaction boundaries now match the business transactions. The changes that are visible in the property editor are described in the following list:
For each EJB module that you work with, you must analyze the business logic in the module and determine what transaction boundaries are implied by the logic. Then use the Transaction Attribute property editor to implement those transaction boundaries. Set the transaction attributes of the enterprise beans, or the individual methods, involved in those transactions to specify the transaction boundaries.
Copyright © 2003, Sun Microsystems, Inc. All rights reserved.