Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getUserTransaction

 
Simon Ingram
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Simon Ingram
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 775
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
C Chavan
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic