• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use UserTransaction in non-EJB code  RSS feed

 
Julie Verne
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in a class that is called from within a Stateless Session bean. In this class, I'm trying to use a UserTransaction object that is created as follows (taken from Zuffoletto's book):



However, when I call tx.begin(), it throws an exception as follows:




Any ideas/suggestions/hints??

Why would this explicitly created tx, being used by one single call be colliding with an existing transaction?

Thanks for your comments. Please post to the forum.

JV

[ October 20, 2004: Message edited by: Julie Verne ]
[ October 20, 2004: Message edited by: Julie Verne ]
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Julie,

It looks like your session bean is already running in the transaction context (is it a CMT session bean). This context gets propogated to all plain java objects that this session bean communicates with.

That's why when your plain java object tries to run in its own context (i.e. UserTransaction#begin()) your app server throws an error.

To fix the problem do one of the following:

1) make your plain java object run in the same context as the calling bean - it means that you do not need any transaction related code in your class (i.e. no reference to UserTransaction). Obviously, for this to work make sure that your bean has a transaction context.

If it is a BMT sb then:


If it is a CMT sb then ensure that your session bean's method(s) trans-attribute is one of the following: Required/RequiresNew/Mandatory. It will ensure that your session bean method always in the transaction context.


Hope it helps,



[ October 21, 2004: Message edited by: Alex Sharkoff ]
[ October 21, 2004: Message edited by: Alex Sharkoff ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!