我有一个名为 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/