Java Reference
In-Depth Information
A transaction attribute specified in the deployment descriptor controls the scope of a transaction. A
transaction attribute may have one of the following values:
 
Required means that the container ensures that the bean method is invoked with a transaction.
If the calling client has a transaction, the container passes it to the bean method. If the calling client
does not have a transaction, the container starts one and then invokes the bean method.
 
RequiresNew means that the container always starts a new transaction when invoking the bean
method. If the calling client has a transaction, the container suspends it and starts a new one. This
is not a nested transaction because the outcome of the new transaction has no impact on the
suspended one. If the calling client does not have a transaction, the container creates a new
transaction and invokes the bean method.
 
Mandatory states that the calling client must have a transaction, which is propagated to the bean
method being invoked. If the calling method does not have a transaction, the container throws a
javax.transaction. TransactionRequiredException .
 
NotSupported means that the method cannot handle transactions. If the calling client has a
transaction, the container suspends it before invoking the bean method. If the calling client does
not have a transaction, the container immediately invokes the bean method.
 
Supports states that the bean method accepts a transaction if available but does not require the
container to create a new one. If the calling client has a transaction, the container propagates it to
the bean method. If the calling client does not have a transaction, the container just invokes the
bean method.
 
Never means that the bean method is not expecting a transaction. If the calling client has a
transaction, the container throws a java.rmi.RemoteException . If the calling client does not
have a transaction, the container just invokes the bean method.
Table 20-2 summarizes the behavior of the container for each of these transaction attributes.
Table 20-2: Transaction Attributes
Transaction Attribute
Client Has Transaction
Client Has No Transaction
Required
Transaction Propagated
New Transaction Started
RequiresNew
Transaction Suspended
New Transaction Started
Mandatory
Transaction Propagated
Throws
TransactionRequiredExceptio
n
NotSupported
Transaction Suspended
No Action
Supports
Transaction Propagated
No Action
Never
Throws RemoteException
No Action
Because transaction attributes are stored in the deployment descriptor, they can be changed during
several phases of J2EE application development: EJB creation, application assembly, and deployment.
However, an enterprise bean developer is responsible for specifying the attributes when the bean is first
created. The attributes should be modified only by other developers who are assembling components
into larger applications. Do not expect the person who is deploying the J2EE application to specify the
transaction attributes.
You can specify the transaction attributes for the entire enterprise bean or for individual methods. If
you've specified one attribute for a method and another for the bean, the attribute for the method takes
precedence. As an example, the transaction attribute of the YachtSessionEJB may be specified as
follows:
<enterprise-beans>
<session>
<ejb-name>YachtSessionEJB</ejb-name>
<transaction-type>Container</transaction-type>
</session>
Search WWH ::




Custom Search