Chapter 11. Transactional Behavior of EJB Applications

This chapter is for the Enterprise Bean provider; that is, the person in charge of developing the software components on the server side.

11.1. Declarative Transaction Management

For container-managed transaction management, the transactional behavior of an Enterprise Bean is defined at configuration time and is part of the assembly-descriptor element of the standard deployment descriptor. It is possible to define a common behavior for all the methods of the bean, or to define the behavior at the method level. This is done by specifying a transactional attribute, which can be one of the following:

NotSupported

If the method is called within a transaction, this transaction is suspended during the time of the method execution.

Required

If the method is called within a transaction, the method is executed in the scope of this transaction; otherwise, a new transaction is started for the execution of the method and committed before the method result is sent to the caller.

RequiresNew

The method will always be executed within the scope of a new transaction. The new transaction is started for the execution of the method, and committed before the method result is sent to the caller. If the method is called within a transaction, this transaction is suspended before the new one is started and resumed when the new transaction has completed.

Mandatory

The method should always be called within the scope of a transaction, else the container will throw the TransactionRequired exception.

Supports

The method is invoked within the caller transaction scope; if the caller does not have an associated transaction, the method is invoked without a transaction scope.

Never

The client is required to call the bean without any transaction context; if it is not the case, a java.rmi.RemoteException is thrown by the container.

This is illustrated in the following table:

Transaction AttributeClient transactionTransaction associated with enterprise Bean's method
NotSupported

-

T1

-

-

Required

-

T1

T2

T1

RequiresNew

-

T1

T2

T2

Mandatory

-

T1

error

T1

Supports

-

T1

-

T1

Never

-

T1

-

error

In the deployment descriptor, the specification of the transactional attributes appears in the assembly-descriptor as follows:

<assembly-descriptor>
  <container-transaction>
    <method>
    <ejb-name>AccountImpl</ejb-name>
    <method-name>*</method-name>
    </method>
    <trans-attribute>Supports</trans-attribute>
  </container-transaction>
  <container-transaction>
    <method>
    <ejb-name>AccountImpl</ejb-name>
    <method-name>getBalance</method-name>
    </method>
    <trans-attribute>Required</trans-attribute>
  </container-transaction>
  <container-transaction>
    <method>
    <ejb-name>AccountImpl</ejb-name>
    <method-name>setBalance</method-name>
    </method>
    <trans-attribute>Mandatory</trans-attribute>
  </container-transaction>
</assembly-descriptor>

In this example, for all methods of the AccountImpl bean which are not explicitly specified in a container-transaction element, the default transactional attribute is Supports (defined at the bean level), and the transactional attributes are Required and Mandatory (defined at the method-name level) for the methods getBalance and setBalance respectively.