Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction between 2 methods in the same bean

 
Gus Mus
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gus Mus
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for wonderful reply
 
HanMing Low
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks.

Han Ming
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic