web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Declarative transaction management in Enterprise JavaBeans With declarative transaction management, the EJB container manages most if not all aspects of the transaction for you. The EJB container handles starting and ending the transaction, plus maintains its context throughout the life of the transaction object. This greatly simplifies an application developer's responsibilities and tasks, especially for transactions in distributed environments. Understanding bean-managed and container-managed transactions When an EJB programmatically performs its own transaction demarcation as part of its business methods, that bean is considered to be using bean-managed transaction. On the other hand, when the bean defers all transaction demarcation to its EJB container, and the container performs the transaction demarcation based on the Application Assembler's deployment instructions, then the bean is referred to as using containermanaged transaction. EJB session beans, both stateful and stateless varieties, can use either container- or bean-managed transactions. However, a bean cannot use both types of transaction management at the same time. EJB entity beans can only use container-managed transaction. It is the bean provider who decides the type of transaction which an EJB can use. An EJB can manage its own transaction if it wishes to start a transaction as part of one operation and then finish the transaction as part of another operation. However, such a design might be problematic if one operation calls the transaction starting method, but no operation calls the transaction ending method. Whenever possible, enterprise beans should use container-managed transactions as opposed to bean-managed transactions. Container-managed transactions require less programming work and are less prone to programming error. In addition, a containermanaged transaction bean is easier to customize and compose with other beans. Local and Global transactions A transaction involves an atomic unit of work performed against data maintained by one or more resource managers. Examples of resource managers are database managements systems and JMS message providers. A local transaction involves work performed against a single resource manager independent of an external transaction manager. For instance, a JDBC connection obtained from a database can have SQL operations performed on it to update the database and then have the work committed in a local transaction using a commit() operation, assuming autoCommit mode for the connection is turned off, otherwise each operation is performed within a local transaction. A global transaction is coordinated by a transaction manager, such as the partition Transaction Manager or OTS engine, and it can involve work performed for one or more distributed resource managers. Transaction management for EJBs, both container-managed and bean-managed, implies use of global transactions. When a single resource manager participates in a global transaction, all work may be performed within a local transaction on behalf of the global transaction (refer to EJB Specification Version 2.0, Section 17.6.4 Local transaction optimization for more details). Methods of an EJB defined with bean-managed transactions must obtain an implementation handle to JTA interface javax.transaction.UserTransaction and invoke operations on it to explicitly participate in a global transaction. With container-managed transactions, the EJB Container interposes each EJB method call and follows certain rules to determine whether or not work should be processed as part of a global transaction. The decision taken by the Container depends on the transaction attribute value set for the method, by the Application Assembler in the components deployment descriptor, and whether a global transaction context exists Chapter 19: Transaction management 175

Declarative transaction management in Enterprise JavaBeans upon invocation of the method (refer to Table 14 in EJB Specification Version 2.0, Section 17.6.2.7 Transaction attribute summary). Should the method be processed without the presence of a global transaction context, work performed against an external resource manager from within the method is completed using local transaction(s). The following are specific examples of when local transactions are used for EJB methods of an EJB with container-managed transaction demarcation: ■ ■ ■ If the transaction attribute is set to NotSupported and the container detects that resources were accessed. If the transaction attribute is set to Supports and the container detects that a) the method was not invoked from within a global transaction, and b) resources were accessed. If the transaction attribute is set to Never and the container detects that resources were accessed. Transaction attributes EJBs that use bean-managed transaction have transaction attributes associated with each method of the bean. The attribute value tells the container how it must manage the transactions that involve this bean. There are six different transaction attributes that can be associated with each method of a bean. This association is done at deployment time by the Application Assembler or Deployer. These attributes are: ■ Required: This attribute guarantees that the work performed by the associated method is within a global transaction context. If the caller already has a transaction context, then the container uses the same context. If not, the container begins a new transaction automatically. This attribute permits easy composition of multiple beans and co-ordination of the work of all the beans using the same global transaction. ■ ■ ■ ■ ■ RequiresNew: This attribute is used when the method does not want to be associated with an existing transaction. It ensures that the container begins a new transaction. Supports: This attribute permits the method to avoid using a global transaction. This must only be used when a bean's method only accesses one transaction resource, or no transaction resources, and does not invoke another enterprise bean. It is used solely for optimization, because it avoids the cost associated with global transactions. When this attribute is set and there is already a global transaction, the EJB Container invokes the method and have it join the existing global transaction. However, if this attribute is set, but there is no existing global transaction, the Container starts a local transaction for the method, and that local transaction completes at the end of the method. NotSupported: This attribute also permits the bean to avoid using a global transaction. When this attribute is set, the method must not be in a global transaction. Instead, the EJB Container suspends any existing global transaction and starts a local transaction for the method, and the local transaction completes at the conclusion of the method. Mandatory: It is recommended that this attribute not be used. Its behavior is similar to Requires, but the caller must already have an associated transaction. If not, the container throws a javax.transaction.TransactionRequiredException. This attribute makes the bean less flexible for composition because it makes assumptions about the caller's transaction. Never: It is recommended that this attribute not be used. However, if used, the EJB Container starts a local transaction for the method. The local transaction completes at the conclusion of the method. 176 BES Developer’s Guide

Declarative transaction management in Enterprise JavaBeans<br />

upon invocation of the method (refer to Table 14 in EJB Specification Version 2.0,<br />

Section 17.6.2.7 Transaction attribute summary). Should the method be processed<br />

without the presence of a global transaction context, work performed against an<br />

external resource manager from within the method is completed using local<br />

transaction(s). The following are specific examples of when local transactions are used<br />

for EJB methods of an EJB with container-managed transaction demarcation:<br />

■<br />

■<br />

■<br />

If the transaction attribute is set to NotSupported and the container detects that<br />

resources were accessed.<br />

If the transaction attribute is set to Supports and the container detects that a) the<br />

method was not invoked from within a global transaction, and b) resources were<br />

accessed.<br />

If the transaction attribute is set to Never and the container detects that resources<br />

were accessed.<br />

Transaction attributes<br />

EJBs that use bean-managed transaction have transaction attributes associated with<br />

each method of the bean. The attribute value tells the container how it must manage<br />

the transactions that involve this bean. There are six different transaction attributes that<br />

can be associated with each method of a bean. This association is done at deployment<br />

time by the Application Assembler or Deployer.<br />

These attributes are:<br />

■<br />

Required: This attribute guarantees that the work performed by the associated<br />

method is within a global transaction context. If the caller already has a transaction<br />

context, then the container uses the same context. If not, the container begins a new<br />

transaction automatically. This attribute permits easy composition of multiple beans<br />

and co-ordination of the work of all the beans using the same global transaction.<br />

■<br />

■<br />

■<br />

■<br />

■<br />

RequiresNew: This attribute is used when the method does not want to be associated<br />

with an existing transaction. It ensures that the container begins a new transaction.<br />

Supports: This attribute permits the method to avoid using a global transaction. This<br />

must only be used when a bean's method only accesses one transaction resource,<br />

or no transaction resources, and does not invoke another enterprise bean. It is used<br />

solely for optimization, because it avoids the cost associated with global<br />

transactions. When this attribute is set and there is already a global transaction, the<br />

EJB Container invokes the method and have it join the existing global transaction.<br />

However, if this attribute is set, but there is no existing global transaction, the<br />

Container starts a local transaction for the method, and that local transaction<br />

completes at the end of the method.<br />

NotSupported: This attribute also permits the bean to avoid using a global<br />

transaction. When this attribute is set, the method must not be in a global<br />

transaction. Instead, the EJB Container suspends any existing global transaction<br />

and starts a local transaction for the method, and the local transaction completes at<br />

the conclusion of the method.<br />

Mandatory: It is recommended that this attribute not be used. Its behavior is similar to<br />

Requires, but the caller must already have an associated transaction. If not, the<br />

container throws a javax.transaction.TransactionRequiredException. This attribute<br />

makes the bean less flexible for composition because it makes assumptions about<br />

the caller's transaction.<br />

Never: It is recommended that this attribute not be used. However, if used, the EJB<br />

Container starts a local transaction for the method. The local transaction completes<br />

at the conclusion of the method.<br />

176 BES Developer’s Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!