File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Bean stuff  in ejbCreate of stateful session bean 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 "Bean stuff  in ejbCreate of stateful session bean" Watch "Bean stuff  in ejbCreate of stateful session bean" New topic
Author

Bean stuff in ejbCreate of stateful session bean

muthu kumaran
Ranch Hand

Joined: Oct 26, 2004
Posts: 56
I have some doubts that could be in the 'ejbCreate' method
of Stateful session beans.

HFEJB mentions that
'setting rollbackonly' and getting the status of rollback(CMT) cannot be done in ejbCreate (thro' SessionContext) whereas these operations could be performed using 'UserTransactions' in BMT.

What is the reason behind this?

Thanks for the help!!
Muthu
Arun Natarajan
Ranch Hand

Joined: Jul 21, 2006
Posts: 30
I am a little confused about this too. Looks like with CMT the stateful bean's ejbCreate() is run in an unknown tx context and so you cannot do a rollback. There is no container transaction to rollback.

However, with BMT during ejbCreate() you can lookup your own UserTransaction(you would usually not do this, but since there is a client associated you have the option of creating a tx) and hence call rollback on the tx you created.
Not sure this helps, anyone with a better explanation??
Joy Mookerji
Ranch Hand

Joined: Jul 26, 2006
Posts: 49
See the Ejb 2.0 Spec (Page 76 Section 7.5.7)Transaction Context of session bean methods:

A session beans newInstance(),setSessionContext(),ejbCreate(),ejbActivate() ejbPassivate() and ejbRemove() are called in an unspecified transaction context since there is no transaction you cannot set it to roll back
setRollbackOnly() and also neither you can check whether it has been set to roll back only getRollbackOnly()


SCJP 5<br />Brainbench Certified in C++<br />PMP<br />Dallas,TX
Charlie A
Greenhorn

Joined: Apr 17, 2005
Posts: 19
Muthu,

Thanks for posting a good question.

HFEJB mentions that
'setting rollbackonly' and getting the status of rollback(CMT) cannot be done in ejbCreate (thro' SessionContext) whereas these operations could be performed using 'UserTransactions' in BMT.


You can call the getUserTransaction() method in ejbCreate(), for both Stateful & Stateless session beans (BMT beans only). The return type of this method will be an object of type UserTransaction.

Only for Stateful session beans(BMT), in ejbCreate(), you can access UserTransaction methods, but not all the methods. UserTransaction methods are not avaible in session beans, since there is no client associated with the bean, during creation.

The Spec says (Pg- 81):

Additional restrictions:
The getRollbackOnly and setRollbackOnly methods of the SessionContext
interface should be used only in the session bean methods that execute in the context of a transaction.
The Container must throw the java.lang.IllegalStateException if the
methods are invoked while the instance is not associated with a transaction.


So it is confirmed that you cannot call setRollBackOnly() methods in ejbCreate(), irrelevant of whether the bean is stateful/stateless or CMT/BMT.

You can refer the tables in page 80 & 90 of the spec.


When the world says, "Give up,"<br />Hope whispers, "Try it one more time."
Charlie A
Greenhorn

Joined: Apr 17, 2005
Posts: 19
Just corrected the typo error of the previous blog

Only in Stateful session beans(BMT), in ejbCreate(), you can access UserTransaction methods, but not all the methods. UserTransaction methods are not avaible in stateless** session beans, since there is no client associated with the bean, during creation.
muthu kumaran
Ranch Hand

Joined: Oct 26, 2004
Posts: 56
Thanks everyone for the info.


Only in Stateful session beans(BMT), in ejbCreate(), you can access UserTransaction methods, but not all the methods.


Does the spec specify which methods can be accessed and which cannot?

Thanks,
Charlie A
Greenhorn

Joined: Apr 17, 2005
Posts: 19
Does the spec specify which methods can be accessed and which cannot?

Yes, the Spec provide these details. Please refer the tables in Pg-80,90 of the spec.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean stuff in ejbCreate of stateful session bean
 
Similar Threads
Looking at code how to differentiate between Stateless and Stateful
Amusing...
legal ejbCreate method
overloading ejbCreate() methods
LifeCycle of a Bean