*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes When is a Bean Discarded? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "When is a Bean Discarded?" Watch "When is a Bean Discarded?" New topic
Author

When is a Bean Discarded?

Claire Simpson
Greenhorn

Joined: Dec 04, 2004
Posts: 8
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
k space
Ranch Hand

Joined: Jun 25, 2002
Posts: 104
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???


The TransactionRequiredLocalException is thrown by the Container - not by the bean itself. Actually, the bean does not get involved in this situation.


SCEA | SCBCD | SCWCD | SCJP - The kSpace
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
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.

Thanks

-- Ravi
[ March 30, 2005: Message edited by: Ravindra Janapareddy ]

SCJP, SCJD, SCWCD, SCBCD, SCEA
Poobhathy Kannan
Ranch Hand

Joined: May 26, 2004
Posts: 94
What about the Answer a? Won't that client tx will be rolled back?


http://learnertobeginner.blogspot.com/
Claire Simpson
Greenhorn

Joined: Dec 04, 2004
Posts: 8
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: When is a Bean Discarded?