• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to know that I am in a transaction context

 
Christophe Grosjean
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have the following problem : some code is run in the context of an SessionBean or a Message-Driven Bean.
In the first case, the code assume that I am in bean-managed transaction, so i mark the transaction bounds manually. In the second one, the transaction is container managed.
I have a portion of code that starts a new UserTransaction and do other things... I'd like to re-use this code in the context of the MDB but I'd like to modify it in order to decide or not to start a new UserTransaction (as I should not in case of MDB)...
How can I now that a UserTransaction is currently running ?
The only way I found until now is to do like :
UserTransaction tran = ... ;
boolean transactionAlreadyRunning = false;
try {
tran.begin();
} catch (NotSupportedException) {
transactionAlreadyRunning = true;
}
Any other idea ?
Christophe
 
Vinod John
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christophe Grosjean:

How can I now that a UserTransaction is currently running ?
Christophe

Did you check UserTransaction.getStatus() method ?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinod John:

Did you check UserTransaction.getStatus() method ?

Just as "VJ" (do people call you that? ) mentioned, you can call UserTransaction.getStatus() which should return javax.transaction.Status.STATUS_NO_TRANSACTION if the current thread has no transaction context.
 
Christophe Grosjean
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I'll try that !
Christophe
 
Vinod John
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:

do people call you that?

You are the 1st person you have that previlage

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!