I have just completed a mock exam questions as follows
Given: Bean A with transaction attributes of "NotSupported" for all its methods. Bean B with transaction attributes of "Mandatory" for all its methods. A client having a transaction context calls a method on bean A, which in turns calls a method on Bean B. Assuming all invokations are local, which of the following statements is correct?
Select 1 correct option. a The client's transaction will be marked for roll back.
b The client will get a javax.ejb.EJBException but it can continue with the same transaction.
c The client will get a javax.ejb.TransactionRolledbackLocalException
d The bean B instance will be discarded.
e None of the above.
The answer given is B which I agree is correct. Here is the explanation
Since bean B method's transaction attribute is Mandatory, it means that its caller must have a transaction context. In this case, its caller is bean A's method. Since its transaction attribute is NotSupported, it does not execute within a transaction context. Therefore, when it calls B's method, it will get a javax.ejb.TransactionRequiredLocalException. Since this exception extends from EJBException, it is a system exception. This means that bean A method encounters a system exception and thus the bean A instance will be discarded. There is no impact on bean B instance. Further, since A's method's transaction attribute is NotSupported, the client's transaction was suspended during the execution of the method. So even though A's method encounters a system exception, the client's transaction will not be marked for a rollback. A system exception in a bean's method translates to an EJBException for a local client and a RemoteException for a remote client. Therefore only option 2 is correct.
What I don't understand is why is bean B not discarded??? It throws a System exception in TransactionRequiredLocalException so why isn't it discarded???
Client executing under txC calls bean A. The bean A method is declared with a tx attribute "not supported" so container associates an undefined transaction context with bean A method. bean A calls bean B. Bean B method is declared wth a tx attribute "mandatory". The Container throws a TxRequiredLocalException to bean A. The bean instance A is discarded. Bean B is not in the picture at all.
-- Ravi [ March 30, 2005: Message edited by: Ravindra Janapareddy ]
Thanks for the answers guys. It all makes sense now!
BTW the client tx is not rolled back because its tx is suspended when it calls the Bean A method with a tx attribute Not Supported. So the client's tx is completely independent from what happens subsequently.