Distributed Transactions   «Prev  Next»

Lesson 9 Bean interactions with different transaction controls
ObjectiveDescribe how multiple beans interact with each other.

Bean Interactions with Different Transaction Controls

Describe how multiple beans with different transaction controls interact with each other

Learn by example 1

Consider a situation where EJB1 has the control REQUIRES_NEW, EJB2 has the control SUPPORTS, and EJB3 has the control MANDATORY.
Take a moment to predict what would happen. When you think that you understand the outcome, ask yourself what would happen if EJB3 were declared TX_NOT_SUPPORTED? Once you think you know the answer, you can look at the answer that follows.

Client interacts with multiple EJBs
Client interacts with multiple EJBs

The following events would occur:
  1. EJB1 creates a new transaction T1.
  2. EJB2 becomes part of T1 as the transaction is propagated.
  3. EJB3 also becomes part of T1.
  4. If there are any problems or rollback is requested, all will be rolled back.
In the example, if EJB3 were declared TX_NOT_SUPPORTED, transaction T1 would be suspended before EJB3 was invoked.

Learn by example 2


Look at the diagram that follows. When you think you understand what will happen, ask yourself the following questions:
  1. What would happen if T2 failed?
  2. What would happen if EJB5's control were NOT_SUPPORTED, or SUPPORTS, or MANDATORY?

Client interacts with four EJBs
Client interacts with four EJBs

If T2 failed, for whatever reason, EJB4 would not be invoked.
T2 would be rolled back. T1 would either commit successfully or it would be rolled back. The point is that, whether or not T2 succeeds, T1 will proceed without reference to T2. However, the call to the method in EJB3 from EJB1 will return an exception that indicates that it did not complete successfully. This will be covered in a later lesson on exceptions.
If EJB5's control were NOT_SUPPORTED, no transaction would be started. If it were SUPPORTS, then, as there is no existing transaction, none will be propagated. If it were MANDATORY, then the message from EJB4 would fail, as EJB4 is not executing within a transaction context as required.

EJB 1.1 Specification

Refer to the EJB 1.1 Specification, chapter 11, for a formal set of rules regarding the interaction of multiple beans within multiple transactions.

Bean Interaction Quiz

Click the Quiz link below to test your knowledge of EJB transactions.
Bean Interaction - Quiz
In the next lesson you will learn to set the transaction controls in the DD.