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 getUserTransaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "getUserTransaction" Watch "getUserTransaction" New topic
Author

getUserTransaction

Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
Hi there ranchers!

Why is it that Stateful session beans are able to call getUserTransaction (on their SessionContext) in the ejbCreate/ejbRemove methods i.e. when not in a transaction context but Stateless beans are not able to do the same thing?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Here is my thinking.

getUserTransaction() is invoked before a transaction starts. This method only makes sense in the right context. By context, I mean client context. For a stateful session bean, ejbCreate() is only invoked in response to a client's create() method, so this is the right context. The fact that ejbCreate() is called with an unspecified transaction context is irrelevant, you can invoke getUserTransaction() from ejbCreate(). Remember, the ejbCreate() and ejbRemove() methods of a stateful session bean are like business methods.

But for a stateless session bean, ejbCreate() is invoked when the container needs to create instances in the pool, it is nothing to do with the client (which might not even exist at this time). This is why getUserTransaction() can only be called from a business method of a stateless session bean, ie after ejbCreate().


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Simon Ingram
Ranch Hand

Joined: Nov 30, 2003
Posts: 173
thanks for you answer, Roger. You are saying that a client context is a necessary prerequisite for a successful call to getUserTransaction(). Sorry to press you, but can you think of any reason why this is so?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Let's look at it this way. Which methods might you want to run in a transaction? It's business methods, right? So, it makes sense for the ejbCreate() method of a stateful session bean to get a UserTransaction object and start a transaction (because it is kind of a business method) but it makes no sense for a stateless session bean.

I'm not claiming to know the answer as neither the EJB spec nor the Head First EJB book explains this, but it's my best shot at fuguring it out.
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Simple way to remember it:

- if you have no user it means you have no UserTransaction
- stateless beans can be created by the container any time (without a user)
- stateful beans are only created when a user asks for one

Since for stateless beans ejbCreate/ejbRemove invocations
are made when the *container* wants to invoke them, not
because a user wants to invoke them, so there is no UserTransaction.

EJB spec 17.6.1 is the right stuff to read for bean-managed
transaction demarcation.


Reid - SCJP2 (April 2002)
C Chavan
Greenhorn

Joined: Mar 19, 2004
Posts: 27
BMT session (SL and SF) EJBs CAN call UserTransaction methods in ejbCreate()/ejbRemove().

ref: http://edocs.bea.com/wls/docs70/jta/trxejb.html#1020111
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
A correction to one of my previous posts ...

The getUserTransaction() method can always be called from the ejbCreate() and the ejbRemove() methods of a stateful or a stateless session bean. But for a stateless session bean, you can only do something with that UserTransaction object, call begin(), from a business method. Contrast this with a stateful session bean where you can do something with that UserTransaction object from ejbCreate() and the ejbRemove().

The reason is the same in both cases, you need to be in a client context to call the UserTransaction methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getUserTransaction