How to roll back JTA transaction correctly?
Asked Answered
O

1

5

What is the correct way of rolling back the transaction of a container managed JTA transaction?

I understand this: EJBException when calling entityManager.getTransaction() that I can't get the transaction instance. I read a solution here but I'm not sure whether this is correct.

I'm also aware that, if I throw an exception, the transaction will be rolled back.

But my question is: if I want to (must) use a container managed EntityManager, what's the correct way of rolling back a transaction inside it?

Once answered 4/2, 2013 at 11:51 Comment(0)
M
7

The asnwer on Code Ranch is right, you have to use SessionContext

@Resource
private SessionContext ctx;

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

See also this article.

Massenet answered 4/2, 2013 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.