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
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.