C H A P T E R 7 |
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 individual enterprise beans involved in the transaction. When you create an enterprise bean with the IDE's EJB wizards and specify container-managed transactions, the bean is create with a default Transaction Attribute.
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 the following sequence of commands from the contextual menu: Properties
Transaction Settings property
ellipsis (...) button.
FIGURE 7-1 shows the Transaction Settings Property Editor for an EJB module with the default transaction attribute settings.
There are several things you should notice about this editor and the transaction attribute settings:
Methods that have the Required transaction attribute must execute transactionally. If the method is called without an active transaction, the container starts a new transaction; if the method is called with an active transaction in progress, the container includes the method in the active transaction. This is the default behavior for your enterprise bean methods.
A business transaction in an EJB module often spans several enterprise beans. A common architecture for an EJB module is a single session bean and several entity beans. Clients call the session bean, and then the session bean calls methods of the entity beans in the module. FIGURE 7-2 shows a transaction of this type.
You want the container to recognize the boundaries of this business transaction. You want all of the work performed when a client calls the session bean and the session bean calls several enterprise beans handled as a single database transaction, so that it is either committed or rolled back together. You want the container to open a transaction when a client calls the session bean, and keep the transaction open while the session bean calls one or more entity beans. Eventually, when the last call made to an entity bean returns, and the session bean method called by the client completes, you want the container to commit the transaction. If this happens, all of the work that resulted from the client's call to the session bean is included in a single database transaction.
To program transaction boundaries, you open the Transaction Settings property editor and modify the transaction attributes of the enterprise beans involved in the transaction. To modify transaction attributes:
1. Right-click the EJB module node and choose the following sequence of commands from the contextual menu: Properties Transaction Settings property
ellipsis (...) button.
This opens the Transaction Settings Property Editor.
2. Expand the entity bean nodes.
3. Click a business method of the session bean to select it.
In this example you want the business methods of the session bean to start new transactions whenever they are called, so you want to select the individual methods and change their transaction attribute values.
4. Click the Transaction Attribute field and change the value.
This changes the transaction attribute for the method you selected. Notice that the method name is now followed by the new transaction setting. In this example, the new value is RequiresNew. This instructs the container to open a new transaction whenever this method is called.
5. Continue until you have redefined the transaction boundaries.
In this example, the transaction attributes of the entity bean business methods are set to Mandatory. You want these method to execute within the boundaries of the transactions opened by the session bean methods, and Mandatory tells the container that these methods can only be called if a transaction is already in progress.
FIGURE 7-3 shows the Transaction Settings Property Editor after the Transaction Attributes have been changed. The transaction boundaries now match the business transactions. The changes are:
Notice that the transaction attributes were modified on the method level, and the method nodes now display the new transaction attribute values after the method names.
For each EJB module that you work with, you need to analyze the business logic and determine the different transaction models that are implied by the logic. Then use the Transaction Attribute property editor to implement those transaction models by setting the transaction attributes of the enterprise beans (or their methods) involved in those transactions.
Copyright © 2002, Sun Microsystems, Inc. All rights reserved.