简体   繁体   English

如何正确回滚JTA事务?

[英]How to roll back JTA transaction correctly?

What is the correct way of rolling back the transaction of a container managed JTA transaction? 回滚容器管理的JTA事务的事务的正确方法是什么?

I understand this: EJBException when calling entityManager.getTransaction() that I can't get the transaction instance. 我理解这一点: EJBException在调用entityManager.getTransaction()时我无法获取事务实例。 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? 但我的问题是:如果我想(必须)使用容器管理的EntityManager ,那么在其中回滚事务的正确方法是什么?

The asnwer on Code Ranch is right, you have to use SessionContext Code Ranch上的asnwer是对的,你必须使用SessionContext

@Resource
private SessionContext ctx;

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

See also this article . 另见本文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM