- 论坛徽章:
- 0
|
Here are the definitions of the transaction attributes listed earlier. In a few of the definitions, the client transaction is described as suspended . This means the transaction is not propagated to the enterprise bean method being invoked; propagation of the transaction is temporarily halted until the enterprise bean method returns. To make things easier, we will talk about attribute types as if they were bean types: for example, we'll say "a Required EJB" as shorthand for "an enterprise bean with the Required transaction attribute." The attributes are:
NotSupported
Invoking a method on an EJB with this transaction attribute suspends the transaction until the method is completed. This means that the transaction scope is not propagated to the NotSupported EJB or to any of the EJBs it calls. Once the method on the NotSupported EJB is done, the original transaction resumes its execution.
Figure 16-1
shows that a NotSupported EJB does not propagate the client transaction when one of its methods is invoked.
Figure 16-1. The NotSupported attribute
Supports
This attribute means that the enterprise bean method will be included in the transaction scope if it is invoked within a transaction. In other words, if the EJB or client that invokes the Supports EJB is part of a transaction scope, the Supports EJB and all EJBs accessed by it become part of the original transaction. However, the Supports EJB doesn't have to be part of a transaction and can interact with clients and other EJBs that are not included in a transaction scope.
Figure 16-2a
shows the Supports EJB being invoked by a transactional client and propagating the transaction.
Figure 16-2b
shows the Supports EJB being invoked by a nontransactional client.
Figure 16-2. The Supports attribute
Required
This attribute means that the enterprise bean method must be invoked within the scope of a transaction. If the calling client or EJB is part of a transaction, the Required EJB is automatically included in its transaction scope. If, however, the calling client or EJB is not involved in a transaction, the Required EJB starts its own new transaction. The new transaction's scope covers only the Required EJB and all other EJBs accessed by it. Once the method invoked on the Required EJB is done, the new transaction's scope ends.
Figure 16-3a
shows the Required EJB being invoked by a transactional client and propagating the transaction.
Figure 16-3b
shows the Required EJB being invoked by a nontransactional client, which causes it to start its own transaction.
Figure 16-3. The Required attribute
RequiresNew
This attribute means that a new transaction is always started. Regardless of whether the calling client or EJB is part of a transaction, a method with the RequiresNew attribute begins a new transaction when invoked. If the calling client is already involved in a transaction, that transaction is suspended until the RequiresNew EJB's method call returns. The new transaction's scope covers only the RequiresNew EJB and all the EJBs accessed by it. Once the method invoked on the RequiresNew EJB is done, the new transaction's scope ends and the original transaction resumes.
Figure 16-4a
shows the RequiresNew EJB being invoked by a transactional client. The client's transaction is suspended while the EJB executes under its own transaction.
Figure 16-4b
shows the RequiresNew EJB being invoked by a nontransactional client; the RequiresNew EJB executes under its own transaction.
Figure 16-4. The RequiresNew attribute
Mandatory
This attribute means that the enterprise bean method must always be made part of the transaction scope of the calling client. The EJB may not start its own transaction; the transaction must be propagated from the client. If the calling client is not part of a transaction, the invocation will fail, throwing a javax.ejb.EJBTransactionRequiredException .
Figure 16-5a
shows the Mandatory EJB invoked by a transactional client and propagating the transaction.
Figure 16-5b
shows the Mandatory EJB invoked by a nontransactional client; the method throws an EJBTransactionRequiredException because there is no transaction scope.
Figure 16-5. The Mandatory attribute
Never
This attribute means that the enterprise bean method must not be invoked within the scope of a transaction. If the calling client or EJB is part of a transaction, the Never EJB will throw an EJBException. However, if the calling client or EJB is not involved in a transaction, the Never EJB will execute normally without a transaction.
Figure 16-6a
shows the Never EJB being invoked by a nontransactional client.
Figure 16-6b
shows the Never EJB being invoked by a transactional client; the method throws an EJBException to EJB clients because a client or EJB that is included in a transaction can never invoke the method.
Figure 16-6. The Never attribute
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4864/showart_311756.html |
|