my dog learned polymorphism
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Session Transaction Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Session Transaction Confusion" Watch "Session Transaction Confusion" New topic

Session Transaction Confusion

John Tao

Joined: May 06, 2005
Posts: 4
In HeadFirstEJB p.508 : "Stateless session beans must complete a transaction by the end of the business method in which the transaction was started". Will the following be legal for a stateless session BMT bean :

private UserTransaction ut;

public void mainMethod(){
ut = context.getUserTransaction() ;
this.subMethod() ;

private void subMethod(){

If the above is not legal, what exception will be thrown ?
Thanks for any advices.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
What you have written is illegal for a SLSB. The container will detect this and do the following:
Log this as an application error to alert the system administrator.
Roll back the started transaction.
Discard the instance of the session bean.
Throw the java.rmi.RemoteException to the client if the client is a remote client, or throw the javax.ejb.EJBException if the client is a local client.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Binoj Viswanathan
Ranch Hand

Joined: Feb 25, 2003
Posts: 85
The container will throw javax.txn.NotSupportedException

SCJP 1.4(98%)<br />SCBCD1.3 (100%)<br />SCWCD 1.4 (93%)(w/o prep..n)
Ana Nava

Joined: May 25, 2004
Posts: 24
Originally posted by Binoj Viswanathan:
The container will throw javax.txn.NotSupportedException

javax.transaction.NotSupportedException is a checked exception thrown by UserTransaction.begin() method. So this exception, must be either be handled or declared in

private void subMethod(){
(John Tao code snippet)

If the exception was handled in subMethod() or mainMethod() then, it would wrap and throw it as an EJBException to the container. The container in turn will throw it as RemoteException (if remote) or EJBException (if local) to the client. Hence the client will see it as RemoteException or EJBException.

I would say that javax.transaction.NotSupportedException is an unexpected exception in the client point of view, as the client need not know what type of transaction is used in the bean (Flat Transaction or Nested Transaction).
(In reference to Page 543 of HFEJB)
I agree. Here's the link:
subject: Session Transaction Confusion
jQuery in Action, 3rd edition