spring - 事务服务中的 ConstraintViolationException 没有回滚

标签 spring rollback transactional

我有一个名为 add() 的服务方法,它用 @Transactional 注释。

我调用它,但是当在相应的 DAO 方法中发生 ConstraintViolationException 时,它会回滚事务,即使我指定不回滚也是如此。

我希望 ConstraintViolationException 会被捕获,而不是它会被抛出 NotFoundException 检查异常。

@Override
@Transactional(noRollbackFor = ConstraintViolationException.class)
public User add(User user) throws NotFoundException {
    try {
        result = userDao.add(user);
    } catch (RuntimeException e) {
        throw new NotFoundException("Couldn't find group");
    }
}

有没有办法在不回滚事务的情况下捕获ConstraintViolationException

我正在使用 spring 3.1.1 和 hibernate 3.6。

最佳答案

啊,我知道会发生什么。 ConstraintViolationException 发生在提交时,在执行该方法之后,当您的 add() 方法周围的事务拦截器尝试提交事务时。既然不能提交,显然,事务被回滚了。它不能做任何其他事情。

关于spring - 事务服务中的 ConstraintViolationException 没有回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777666/

相关文章:

sql-server - SQL Server : BEGIN TRAN . .. COMMIT without ROLLBACK 不回滚取决于错误

python - 我应该如何以及在哪里创建此事务方法?

java - 服务同时调用两次

使用独特的 bean 进行 Spring Autowiring : Spring expected single matching bean but found 2

java - 将字符串数据类型限制为 Spring boot 2.4 (Jackson) 中请求主体的字符串类型

java - 为什么 Spring DeferredResult 在我的应用程序中串行执行?

java - @事务: Is there a way that hibernate can persist child transacttion when parent fails

model-view-controller - Autowiring 的 bean 在 MVC Controller 中为空

维克斯。取消安装时回滚自定义操作

mysql - C# 插入所有行还是不插入?