Given a list of transaction behaviors, match them with the appropriate transaction attributes.
A transaction attribute is a value associated with a method of a session or entity bean's home or component interface or with the onMessage(...) method of a message-driven bean. The transaction attribute specifies how the Container must manage transactions for a method when a client invokes the method via the enterprise bean's home or component interface or when the method is invoked as the result of the arrival of a JMS message.
The transaction attribute must be specified for the following methods:
For a SESSION bean, the transaction attributes MUST be specified for the methods defined in the bean's COMPONENT interface and all the direct and indirect superinterfaces of the component interface, excluding the methods of the javax.ejb.EJBObject or javax.ejb.EJBLocalObject interface. Transaction attributes MUST NOT be specified for the methods of a session bean's HOME interface.
For an ENTITY bean, the transaction attributes MUST be specified for the methods defined in the bean's COMPONENT interface and all the direct and indirect superinterfaces of the component interface, excluding the getEJBHome, getEJBLocalHome, getHandle, getPrimaryKey, and isIdentical methods; and for the methods defined in the bean's HOME interface and all the direct and indirect superinterfaces of the home interface, excluding the getEJBMetaData and getHomeHandle methods specific to the remote home interface (remove, create, and find methods can all have transaction attributes).
For a MESSAGE-DRIVEN bean, the transaction attribute MUST be specified for the bean's onMessage(...) method.
Providing the transaction attributes for an enterprise bean is an optional requirement for the Application Assembler, because, for a given enterprise bean, the Application Assembler must either specify a value of the transaction attribute for all the methods for which a transaction attribute must be specified, or the Assembler must specify none. If the transaction attributes are not specified for the methods of an enterprise bean, the Deployer will have to specify them.
Enterprise JavaBeans defines the following (SIX) values for the transaction attribute:
NotSupported
Required
Supports
RequiresNew
Mandatory
Never
For MESSAGE-DRIVEN beans, ONLY the Required and NotSupported transaction attributes may be used.
For ENTITY beans that use EJB 2.0 container-managed persistence, only the Required, RequiresNew, or Mandatory transaction attributes should be used for the methods defined in the bean's component interface and all the direct and indirect superinterfaces of the component interface, excluding the getEJBHome, getEJBLocalHome, getHandle, getPrimaryKey, and isIdentical methods; and for the methods defined in the bean's home interface and all the direct and indirect superinterfaces of the home interface, excluding the getEJBMetaData and getHomeHandle methods specific to the remote home interface. Containers may optionally support the use of the NotSupported, Supports, and Never transaction attributes for the methods of entity beans with container-managed persistence. However, entity beans with container-managed persistence that use these transaction attributes WILL NOT BE PORTABLE.
If an enterprise bean implements the javax.ejb.SessionSynchronization interface, the Application Assembler can specify only the following values for the transaction attributes of the bean's methods: Required, RequiresNew, or Mandatory. This restriction is necessary to ensure that the enterprise bean is invoked only in a transaction. If the bean were invoked without a transaction, the Container would not be able to send the transaction synchronization calls.
Container-managed transaction demarcation for Session and Entity Beans.
NotSupported
The Container invokes an enterprise Bean method whose transaction attribute is set to NotSupported with an unspecified transaction context.
If a client calls with a transaction context, the container SUSPENDS the association of the transaction context with the current thread before invoking the enterprise bean's business method. The container resumes the suspended association when the business method has completed. The suspended transaction context of the client is not passed to the resource managers or other enterprise Bean objects that are invoked from the business method.
If the business method invokes other enterprise beans, the Container passes NO transaction context with the invocation.
Required
The Container must invoke an enterprise Bean method whose transaction attribute is set to Required with a valid transaction context.
If a client invokes the enterprise Bean's method while the client is associated with a transaction context, the container invokes the enterprise Bean's method in the client's transaction context.
If the client invokes the enterprise Bean's method while the client is not associated with a transaction context, the container AUTOMATICALLY STARTS a new transaction before delegating a method call to the enterprise Bean business method. The Container automatically enlists all the resource managers accessed by the business method with the transaction. If the business method invokes other enterprise beans, the Container PASSES the transaction context with the invocation. The Container attempts to COMMIT the transaction when the business method has completed. The container performs the commit protocol before the method result is sent to the client.
Supports
The Container invokes an enterprise Bean method whose transaction attribute is set to Supports as follows:
If the client calls with a transaction context, the Container performs the same steps as described in the Required case (uses client's transaction).
If the client calls without a transaction context, the Container performs the same steps as described in the NotSupported case (with an unspecified transaction context).
RequiresNew
The Container must invoke an enterprise Bean method whose transaction attribute is set to RequiresNew with a NEW transaction context.
If the client invokes the enterprise Bean's method while the client is NOT associated with a transaction context, the container AUTOMATICALLY STARTS a new transaction before delegating a method call to the enterprise Bean business method. The Container automatically enlists all the resource managers accessed by the business method with the transaction. If the business method invokes other enterprise beans, the Container passes the transaction context with the invocation. The Container attempts to commit the transaction when the business method has completed. The container performs the commit protocol before the method result is sent to the client.
If a client calls with a transaction context, the container SUSPENDS the association of the transaction context with the current thread before starting the new transaction and invoking the business method. The container resumes the suspended transaction association after the business method and the new transaction have been completed. 0
Mandatory
The Container MUST invoke an enterprise Bean method whose transaction attribute is set to Mandatory in a client's transaction context. The client is required to call with a transaction context.
If the client calls with a transaction context, the Container performs the same steps as described in the Required case (uses client's transaction).
If the client calls without a transaction context, the Container throws the javax.transaction.TransactionRequiredException exception if the client is a remote client, or the javax.ejb.TransactionRequiredLocalException if the client is a local client. 1
Never
The Container invokes an enterprise Bean method whose transaction attribute is set to Never WITHOUT a transaction context defined by the EJB specification. The client is required to call without a transaction context.
If the client calls with a transaction context, the Container throws the java.rmi.RemoteException exception if the client is a remote client, or the javax.ejb.EJBException if the client is a local client.
If the client calls without a transaction context, the Container performs the same steps as described in the NotSupported case (with an unspecified transaction context). 2
Container-managed transaction demarcation for Message-driven Beans.
Only the NotSupported and Required transaction attributes may be used for message-driven beans. The use of the other transaction attributes is not meaningful for message-driven beans because there can be no pre-existing transaction context (RequiresNew, Supports) and no client to handle exceptions (Mandatory, Never).
NotSupported
The Container invokes a message-driven Bean method whose transaction attribute is set to NotSupported with an unspecified transaction context. 3
If the onMessage(...) method invokes other enterprise beans, the Container passes NO transaction context with the invocation.
Required
The Container must invoke a message-driven Bean method whose transaction attribute is set to Required with a valid transaction context. Because there is never a client transaction context available for a message-driven bean, the container automatically starts a new transaction before the dequeuing of the JMS message and, hence, before the invocation of the message-driven bean's onMessage(...) method. The Container automatically enlists the resource manager associated with the arriving message and all the resource managers accessed by the onMessage(...) method with the transaction. If the onMessage(...) method invokes other enterprise beans, the Container passes the transaction context with the invocation. The Container ATTEMPTS TO COMMIT the transaction when the onMessage(...) method has completed. If the onMessage(...) method does not successfully complete or the transaction is rolled back by the Container, JMS message redelivery semantics apply. 4
Table 11.1. Transaction attribute summary
Transaction attribute | Client's transaction | Transaction associated with business method | Transaction associated with resource managers |
---|---|---|---|
NotSupported | none | none | none |
T1 | none | none | |
Required | none | T2 | T2 |
T1 | T1 | T1 | |
Supports | none | none | none |
T1 | T1 | T1 | |
RequiresNew | none | T2 | T2 |
T1 | T2 | T2 | |
Mandatory | none | ERROR | n/a |
T1 | T1 | T1 | |
Never | none | none | none |
T1 | ERROR | n/a |