How to tell if a transaction is active in a Java EE 6 interceptor?

后端 未结 1 1928
清酒与你
清酒与你 2021-01-02 14:12

I am planning to write an interceptor for an EJB that will do basically the following:

@AroundInvoke
public Object setContext(InvocationContext ctx) throws E         


        
相关标签:
1条回答
  • 2021-01-02 14:43

    You can inject a TransactionSynchronizationRegistry and use getTransactionStatus to get the status of the transaction in the current context, it returns an int which is a contant in the Status class, in your case you are looking for STATUS_NO_TRANSACTION

    Inject:

    @Resource
    TransactionSynchronizationRegistry tsr;
    

    Check Transaction Status:

    if (Status.STATUS_NO_TRANSACTION == tsr.getTransactionStatus()) {
       // no transaction
    }
    
    0 讨论(0)
提交回复
热议问题