Get current JTA transaction status from CMT EJB

You probably know that if you use BMT (Bean Managed Transactions) you can get information about current transaction status by using UserTransaction interface (which implementation can be fetched either by JNDI or using dependency injection) and executing it’s getStatus() method.

If you use CMT (Container Managed Transactions) you cannot use UserTransaction interface. Instead, you can manage your transaction through SessionContext interface. This interface gives you two places that hooks to the current transaction: setRollbackOnly(-) and getRollbackOnly().
But what if you would like to check the current transaction’s status?

Well, the first answer would be: think twice if you really need to do this.

The transaction is container managed, so get/setRollbackOnly(-) should be probably the most important methods for you.

However, if you still need to get some more detailed information about your CMT you can use the TransactionSynchronizationRegistry interface which (just like UserTransaction) can be fetched using JNDI or @Resource annotation:

@Resource
TransactionSynchronizationRegistry txReg;
    
txReg.getTransactionStatus();

Here you can find what the integer status returned by this method invocation means.