How to cancel a JTA transaction?

What is the correct way to rollback a container managed JTA transaction transaction?

I understand this: EJBException when calling entityManager.getTransaction () that I cannot get an instance of the transaction. I read the solution here , but I'm not sure if this is correct.

I also know that if I make an exception, the transaction will be canceled.

But my question is: if I want (should) use a container-driven one EntityManager, what is the correct way to roll back a transaction inside it?

+5
source share
1 answer

Anyer on Code Ranch is right, you should use SessionContext

@Resource
private SessionContext ctx;

//and then in method
if(fail) {
    ctx.setRollBackOnly(); 
}

See also article.

+7
source

All Articles