Java Reference
In-Depth Information
Listing 6.2. Implementing a method requiring an existing transaction
@Stateless(name="CreditCardManager")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CreditCardManagerBean implements CreditCardManager {
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void chargeCreditCard(CreditCard creditCard, BigDecimal amount)
throws CreditProcessingException {
// debit the credit card...
}
}
The @TransactionAttribute annotation tells the container how to handle transac-
tions, including when to start a new transaction, when to join an existing transaction, and so
on. The annotation can be applied to either an individual CMT bean method or to the entire
bean. If the annotation is applied at the bean level, all business methods in the bean inherit
the transaction attribute value specified by it. In listing 6.1 , you specify that the value of
@TransactionAttribute annotation for the placeSnagItOrder method should
be TransactionAttribute.REQUIRED . There are six choices for this annotation
defined by the enumerated type TransactionAttributeType . Table 6.1 summarizes
their behavior.
Table 6.1. Effects of transaction attributes on EJB methods
Transaction attribute
Caller transaction exists?
Effect
REQUIRED
No
Container creates a new transaction.
Yes
Method joins the caller's transaction.
REQUIRES_NEW
No
Container creates a new transaction.
Container creates a new transaction and the
caller's transaction is suspended.
Yes
SUPPORTS
No
No transaction is used.
Yes
Method joins the caller's transaction.
javax.ejb.EJBTransactionRequiredException
is thrown.
MANDATORY
No
Yes
Method joins the caller's transaction.
NOT_SUPPORTED
No
No transaction is used.
The caller's transaction is suspended and the
method is called without a transaction.
Yes
NEVER
No
No transaction is used.
Yes
javax.ejb.EJBException is thrown.
Let's take a look at what each value means and where each is applicable.
 
 
Search WWH ::




Custom Search