【发布时间】:2013-09-06 08:35:54
【问题描述】:
我正在使用带有单个持久性单元的 OpenJPA 2.0。
在我的 persistence.xml 中,我选择使用配置 transaction-type="RESOURCE_LOCAL" 并手动管理事务。
现在,在下面的代码中,如果 PersistenceException 被抛出(并被捕获),我应该如何清理事务?
try {
entityManager.getTransaction().begin();
MyClassPO myClassPO = (MyClassPO) entityManager
.createQuery("select bn from myClassPO bn where bn.xxx = :xxx")
.setParameter("xxx", xxx)
.getSingleResult(); // NoResultException gets thrown here
... do some more stuff ...
entityManager.getTransaction().commit();
} catch (PersistenceException e) {
// what should I do with the open transaction here ??
logger.error(e);
throw new MyOtherException(e);
}
我知道事务没有自动清理,因为我下次运行相同的操作时收到错误消息This operation cannot be performed while a Transaction is active.
是否就像将entityManager.getTransaction().rollback(); 放在catch 块中一样简单?
【问题讨论】:
标签: java transactions persistence openjpa