@Transactional(noRollbackFor = RuntimeException.class)不会阻止RuntimeException上的回滚 [英] @Transactional (noRollbackFor=RuntimeException.class) does not prevent rollback on RuntimeException

查看:898
本文介绍了@Transactional(noRollbackFor = RuntimeException.class)不会阻止RuntimeException上的回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Transactional (noRollbackFor=RuntimeException.class)
public void methodA (Entity e){
   service.methodB(e);
}



<---> ---下面的服务方法---

---service method below---

@Transactional (propagation=Propagation.REQUIRES_NEW, noRollbackFor=RuntimeException.class)
public void methodB (Entity e){
   dao.insert(e);
}

dao.insert(e) in methodB()会导致主键违例,并抛出 ConstraintViolationException ,它是 RuntimeException ,因为我使用的 noRollbackFor 属性,我预计事务仍然会提交。但我观察到外部事务( methodA )仍然由 HibernateTransactionManager 回滚, p>

When dao.insert(e) in methodB() causes a primary key violation and throws a ConstraintViolationException, which is a subclass of RuntimeException, I would expect the transaction to still commit because of the noRollbackFor property I used. But I observed that the outer transaction (on methodA) is still being rolled back by the HibernateTransactionManager with the message


org.springframework.transaction.UnexpectedRollback异常:
事务已回滚,因为它已标记为仅回滚

org.springframework.transaction.UnexpectedRollback Exception: Transaction rolled back because it has been marked as rollback-only

我发现了类似的问题,但并不完全是这一个。

I've found similar questions reported but not exactly this one.

推荐答案

一旦发现异常, Hibernate Session 应该被丢弃,事务应该被回滚:
$ b

Once an exception is caught, the Hibernate Session should be discarded and the transaction should be rolled back:


如果Session引发异常,事务必须是回滚
,会话被丢弃。在发生异常之后,Session
的内部状态可能与数据库不一致。

If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.

因此, noRollbackFor 适用于可能抛出异常的服务和DAO层。假设您有一个通过Hibernate DAO写入数据库的 gatewayService ,并通过 emailService 发送电子邮件。如果emailService引发 SendMailFailureException ,您可以指示网关服务在发现此异常时不回滚:

So, noRollbackFor applies to your Service and DAO layer that might throw an exception. Let's say you have a gatewayService that write to a Database through a Hibernate DAO and also sends an email through an emailService. If the emailService throws a SendMailFailureException you can instruct the gatewayService not to roll back when it will catch this exception:

@Transactional(noRollbackFor=SendMailFailureException.class)
public void saveAndSend(Entity e){
   dao.save(e);
   emailService.send(new Email(e));
}

这篇关于@Transactional(noRollbackFor = RuntimeException.class)不会阻止RuntimeException上的回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆