I don't think you've understood the responsibility of the UserTransaction
class. It does not exist to provide you with access to the current running transaction. It is used to initiate any communication with the Transaction Manager of the container, especially for beginning and ending bean-managed transactions; that is why you must not access it from a container managed transaction context.
I would like to know, is there any other way to get access to getStatus() method?
No, one cannot, atleast not using the EJB APIs. One can at most, use EJB interceptors to log the fact that EJB methods have been invoked. You'll need to be quite intelligent to track state across calls, and then infer the transaction state.
If you are not averse to use Container specific APIs, you might be able to access the underlying transaction. Be forewarned, for the approach listed below might not work if you do not know how to use it, or if the container prohibits you from doing so. The mechanism described below is how the SpringFramework gains access to the JTA transaction manager and is able to create and manage transactions.
In the case of Oracle WebLogic, one can obtain a reference to the TranactionHelper class
, which can be used to obtain a reference to the current transaction associated with the thread, whose status can be obtained. I would point to the sources of the Transaction SPI for JTA in the Spring framework, if you need to pursue this course for other application servers.
STATUS_ACTIVE = 0; STATUS_MARKED_ROLLBACK = 1; STATUS_PREPARED = 2; STATUS_COMMITTED = 3; STATUS_ROLLEDBACK = 4; STATUS_UNKNOWN = 5; STATUS_NO_TRANSACTION = 6; STATUS_PREPARING = 7; STATUS_COMMITTING = 8; STATUS_ROLLING_BACK = 9;
– Meiny