Distributed Transactions   «Prev  Next»

Lesson 8 Container-managed transactions
ObjectiveDescribe how container manages transactions on behalf of bean.

Container-managed Transactions

Container Interposition or Intervention

The container interposes itself between the incoming method call from a client and the actual method on the bean instance itself. Thus, when the message arrives at the EJBObject, the EJBObject can start a transaction on behalf of the bean's method.
The following SlideShow, which uses the BankAccount bean, illustrates the sequence of steps, from the arrival of a message from a client, the starting of the transaction, and the calling of the method, to the return back to the client.
It assumes that the BankAccount bean runs inside a transaction and stores the account balance information on some recoverable resource.

1) Bank Account Bean 1 2) Bank Account Bean 2 3) Bank Account Bean 3 4) Bank Account Bean 4 5) Bank Account Bean 5 6) Bank Account Bean 6 7) Bank Account Bean 7
Container Interposition

Transaction Controls

How does the container know whether to start a transaction? Or whether to start a new transaction or propagate the existing transaction?
Each method has an associated transaction control that is declared in the deployment descriptor. When the EJBObject receives a method call, it checks with the DD for the transaction control that was specified for that method. The EJBObject performs the required operation.


The EJB transaction controls are as follows:
NotSupported Any existing transaction will be suspended before calling the method. The transaction will be resumed when the method returns.
Transactional access to resources will be local to the bean instance. No transactional context will be passed when other EJBs are invoked from this instance.
Required The invocation of the bean's method must be within the context of a transaction. The transaction may be started by client or further upstream. If there is no current transaction, then container will start one. If other beans are invoked, the transaction will be propagated. Transaction will be committed when the method returns.
Supports If the client calls the method within a transaction then it behaves the same as required control that is described above.
If the client calls without a transaction, it is the same as the NotSupported control. This is not the most useful control. Why not just use NotSupported?
RequiresNew If there is an existing transaction, it will be suspended and a new one started. If there is no existing transaction, a new one will be started. The transaction will be committed when the method returns. If there was a suspended transaction, it will be restarted.
Mandatory The client must call the method within the context of an existing global transaction. If there is no existing transaction when the call is made, the EJBObject will throw javax.transaction.TransactionRequiredException.
Never The client must never propagate a global transaction context.

In the next lesson , bean interaction with transactions will be discussed.