Accessing transaction status in container managed beans

Vicky picture Vicky · Jun 5, 2011 · Viewed 8.9k times · Source

I have a @Stateless EJB using container managed transaction. Is there a way to access the "status" of javax.transaction.UserTransaction? That is, calling UserTransaction.getStatus() inside the bean methods?

I know access to UserTransaction is prohibited in container managed beans, but I would like to know, is there any other way to get access to getStatus() method?

Answer

Brett Kail picture Brett Kail · Jun 7, 2011

If you only need to know if the transaction is marked for rollback, then use EJBContext.getRollbackOnly. Otherwise, with JTA 1.1, you can use TransactionSynchronizationRegistry:

TransactionSynchronizationRegistry tsr = (TransactionSynchronizationRegistry)
  new InitialContext().lookup("java:comp/TransactionSynchronizationRegistry");
int status = tsr.getTransactionStatus()