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.
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.
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
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()
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
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.
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--
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().
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 .
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--
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop