posted 19 years ago
Hello
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???
Thanks in anticipation