事务服务中的 ConstraintViolationException 没有回滚 [英] No rollback for ConstraintViolationException in transactional service

查看:50
本文介绍了事务服务中的 ConstraintViolationException 没有回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a service method called add() which is annotated with @Transactional.

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

I call it but when a ConstraintViolationException occurs inside corresponding DAO method it'll rollback the transaction even when I specify not to.

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

I expect that ConstraintViolationException to be caught and instead of it NotFoundException checked exception would be thrown.

@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?

Is there a way to catch ConstraintViolationException without transaction rollback?

我使用的是 spring 3.1.1 和 hibernate 3.6.

I'm using spring 3.1.1 and hibernate 3.6.

推荐答案

啊,我明白了.ConstraintViolationException 在提交时发生,在方法执行之后,当围绕您的 add() 方法的事务拦截器尝试提交事务时.由于它无法提交,显然,事务被回滚.它不能用于其他任何东西.

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. Since it can't commit, obviously, the transaction is rollbacked. It can't to anything else.

这篇关于事务服务中的 ConstraintViolationException 没有回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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