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.
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
}