[英]No rollback for ConstraintViolationException in transactional service
I've a service method called add()
which is annotated with @Transactional
. 我有一个名为add()
的服务方法,它使用@Transactional
注释。
I call it but when a ConstraintViolationException occurs inside corresponding DAO method it'll rollback the transaction even when I specify not to. 我调用它,但是当相应的DAO方法内发生ConstraintViolationException时,即使我指定不执行,它也会回滚事务。
I expect that ConstraintViolationException
to be caught and instead of it NotFoundException
checked exception would be thrown. 我希望捕获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");
}
}
Is there a way to catch ConstraintViolationException
without transaction rollback? 有没有办法在没有事务回滚的情况下捕获ConstraintViolationException
?
I'm using spring 3.1.1 and hibernate 3.6. 我正在使用spring 3.1.1和hibernate 3.6。
Ah, I see what happens. 啊,我看到会发生什么。 The ConstraintViolationException happens at commit time, after the method has been executed, when the transaction interceptor around your add()
method tries to commit the transaction. ConstraintViolationException在提交时,在执行方法之后,当add()
方法周围的事务拦截器尝试提交事务时发生。 Since it can't commit, obviously, the transaction is rollbacked. 由于它无法提交,显然,事务是回滚的。 It can't to anything else. 它不能做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.