Accessing transaction status in container managed beans
Asked Answered
V

2

7

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?

Valuation answered 5/6, 2011 at 3:49 Comment(0)
M
2

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.

Marja answered 5/6, 2011 at 7:11 Comment(0)
S
16

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()
Sirenasirenic answered 7/6, 2011 at 1:6 Comment(1)
Just to complement, transaction status values are defined in the Status class. To summarize: 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
M
2

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.

Marja answered 5/6, 2011 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.