12

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.

marcus
  • 5,041
  • 3
  • 30
  • 36

1 Answers1

22

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
}
Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
  • 1
    Thanks, I would never have guessed by myself! It works fine with a small change: **@Resource(lookup="java:comp/TransactionSynchronizationRegistry")** or **@Resource(mappedName="java:comp/TransactionSynchronizationRegistry")**. Without the parameter, @Resource would construct a JNDI name based on my class name and variable name. – marcus Dec 26 '12 at 14:17
  • I guess that depends on the applicaiton server you are using – Aviram Segal Dec 26 '12 at 14:20
  • Yeah, I'm using JBoss AS 7.1. The JNDI name seems to be portable, though, since it's in the JavaDoc. – marcus Dec 26 '12 at 14:31