jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes Transaction between 2 methods in the same bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Transaction between 2 methods in the same bean" Watch "Transaction between 2 methods in the same bean" New topic

Transaction between 2 methods in the same bean

Gus Mus
Ranch Hand

Joined: Feb 28, 2003
Posts: 53
For instance I have one Stateless Session Bean with Container Managed Transaction, which consists of 2 Methods, lets say A() and B() For some reasons the transaction attribute for method A() is Required and for method B() is RequiresNew. The problem is method A() call method B() in the same bean and inside container,
the question is will Container begin new Transaction for method B() ? (Because it's Requires new ?)
I am wondering ... because Container start the new transaction by EJBObject (that decorates the Stateless Bean) and when client call method A() of the bean then container start the transaction, but method A in the same bean call method B then how container know if it needs to start the new transaction ?
Thank you for any help
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
If you use this.methodB() or its equivalent (just use methodB()) then the container won't know -- but that's not the way you're supposed to call methods in the same bean. What you have to do is:
(1) Get the EJBContext of the bean
(2) Get the EJBObject from the context
(3) Cast the EJBObject to the Remote Interface
(4) Call methodB() on the newly cast object.
This is described in Richard Monson-Haefel's book in detail with code. This way the container DOES know about the call, and can start the second transaction accordingly.

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Gus Mus
Ranch Hand

Joined: Feb 28, 2003
Posts: 53
Thank you for wonderful reply
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi Kyle,
Can you point me to the page in the book where this is reference?
I'm having the 3rd Edition but could not locate it.
Sorry for the request.

Han Ming
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I've lost count of the number of books and articles I've read on EJB lately, and I have never seen this mentioned anywhere. I just ran up against this last week, and I had to create a reproducable test case for BEA to take a look at it. After a few hours of going over the spec with an engineer, it was concluded that the spec is unclear.
He suggested the same solution presented above, and it works. However, now that I know the solution and why it works, I understand why it shouldn't have worked the way I was doing it.
In any case, since you're sitting in the bean itself, you can go through the local interface instead of the remote:

I just wish I had found this message last week.
I agree. Here's the link: http://aspose.com/file-tools
subject: Transaction between 2 methods in the same bean
It's not a secret anymore!