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

筅森魡賤 提交于 2019-11-30 04:40:15

问题


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

@AroundInvoke
public Object setContext(InvocationContext ctx) throws Exception {
    em.createQuery("... set something [database specific] ...").getSingleResult();
    try {
        return ctx.proceed();
    } finally {
        em.flush();
        em.createQuery("... unset something [database specific] ...").getSingleResult();
    }
}

The problem is that em.flush() throws an exception if it is applied to a method annotated with @TransactionAttribute(NOT_SUPPORTED) or @TransactionAttribute(SUPPORTS) (i.e. read-only methods), while it works fine with methods using the default @TransactionAttribute(REQUIRED) (i.e. methods that change the database).

Is there a way to tell whether a transaction is active in order to avoid the call to em.flush() when there is no transaction currently running, or must I create two interceptors, one with em.flush() and one without it?

I cannot inject a UserTransaction since the bean uses container-managed transactions. EntityManager's (em) transaction methods only work with resource_local transactions, not with JTA transactions. I need the em.flush() call to be sure that the unset query is the last thing that runs.

As bonus question, does anyone knows if what I am trying to do is some kind of bad practice? I know that the interceptor runs in the same context as the bean, so it will be safe to share things via the database session, but I must be really careful to unset everything on exit because connections are pooled in the container.


回答1:


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
}


来源:https://stackoverflow.com/questions/14004504/how-to-tell-if-a-transaction-is-active-in-a-java-ee-6-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!