• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Obtaining transction reference for stateless session beans

 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot use the SessionContext of a stateless session bean to invoke getUserTransaction() from ejbCreate() or ejbRemove(). But you can do this for a stateful session bean. Does anyone know why? I'm guessing that the answer is to do with a stateful session bean being able to keep multiple transactions open across multiple clients in a beans-managed transaction demarcation.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stateless S B
In stateless session bean ejbCreate() and ejbRemove() are handled by the Container not the user/client that is creation and removal of beans is container job. Whenever client calls a create() method on home the container justgrabs from the bean-ready pool and hands it to the client.
Hence you don't have UserTransaction in ejbCreate() and ejbRemove()

Stateful S B
In Stateful session ejbCreate() and ejbRemove() are invoked when the user invokes create() on the home Interface. That is ejbCreate() and ejbRemove() are the consequence of user invoking create() and remove() methods.

Hence you have UserTransaction in ejbCreate() and ejbRemove()

If you look at the life cycle diagram it is easy to understand.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I had a mental block. Naturally, you can't get client-related information for a stateless session bean!
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry to jump in the discussion, but just wanted to make sure that I understood the statelesee beans correctly.
setSessionContext is called for all session EJBs, for stateful and stateless beans. So the stateless session bean can still reference the transaction via SessionContext.getUserTransaction().
Is this correct ?
Thanks
Tejas
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only instances of a bean with bean-managed transaction demarcation
can use getUserTransaction(). What you also need to know is when this method can be called.
For stateful session beans
==========================
ejbCreate()
ejbRemove()
ejbActivate()
ejbPassivate()
For stateless session beans
===========================
ejbCreate()
ejbRemove()
For message-driven beans
========================
onMessage()
 
Tejas Bavishi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roger,
Just searched the EJB 2.0 spec..... and have to disagree with your post.
As per the EJB 2.0 spec, getUserTransaction() method cannnot be called in the setSessionContext(), ejbCreate(), ejbRemove(), ejbActivate(), ejbPassivate() methods of session beans. See section 7.5.7. These methods have unspecified transaction context.
This kind of makes sense as when a business method is invoked on a session bean instance, a transaction context will be associated to that bean instance.
Thanks
Tejas
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reading of the spec is incorrect. The getUserTransaction() method can be called from all the methods I have listed. They run in an unspecified transaction context, see 17.6.5 for details.
Also, bear in mind that a BMT bean will never use any other bean's transaction. What happens is that should a BMT bean's method be called in a transaction, then that transaction will be suspended and the BMT bean will create and run in its own transaction. Once the BMT bean's transaction is finished, the original transaction can resume.
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been discussed here before, but I can't find it :-(
So I'll have to try and think (smoke comes from ears as he ponders...)
Ah, yes, you can call getUserTransaction() from ejbCreate and ejbRemove of a stateless BMT bean, BUT... you can't DO anything with it!
The table in the spec, page 90, shows that you can just do java:comp/env stuff with ejbCreate and ejbRemove, whereas when you move down to the business method cell, below, you get "User Transaction Methods".
This is in agreement with the HFE book page 228 (under ejbCreate and ejbRemove have "get a transaction reference, and call methods on it (BMT beans)" grayed-out.
So from my perspective, for stateless session beans (BMT or CMT) are the same, from an access standpoint, if you club together getting a user transaction with actually using it.
--Dale--
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ah, yes, you can call getUserTransaction() from ejbCreate and ejbRemove of a stateless BMT bean, BUT... you can't DO anything with it!


If you look at CMT and BMT stateless session beans with respect to what can be done in the ejbCreate() and ejbRemove() methods, the only difference is that the SessionContext's getUserTransaction() can be called for BMT. What is the reason for this? I suspect it is to enable methods such as the UserTransaction's begin() to be invoked in order to start a transaction. Subsequently, business methods can be called until the transaction is completed with a call to commit() or rollback(). Of course, these business methods cannot be called from ejbCreate().
 
Santosh Ramachandrula
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is that ejbCreate() and ejbRemove() have access to geUserTransaction() even though the Container is repsonsible for these methods in SLSB/MDB with BMT
After this discussion I am kind of curious to know that what is the use of getUserTransaction() in SLSB/MDB ejbCreate()and ejbremove() with BMT .
 
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:

If you look at CMT and BMT stateless session beans with respect to what can be done in the ejbCreate() and ejbRemove() methods, the only difference is that the SessionContext's getUserTransaction() can be called for BMT. What is the reason for this? I suspect it is to enable methods such as the UserTransaction's begin() to be invoked in order to start a transaction.


In that earlier discussion thread (that I couldn't find), I think it included someone giving your 'begin' scenario a try, and finding that it throws an exception. Or at least trying to call something on the user transaction. It might be easier to run an example than to locate that old thread!
--Dale--
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic