No rollback for ConstraintViolationException in transactional service

梦想与她 提交于 2019-11-30 18:20:32

问题


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

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

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");
    }
}

Is there a way to catch ConstraintViolationException without transaction rollback?

I'm using spring 3.1.1 and hibernate 3.6.


回答1:


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.



来源:https://stackoverflow.com/questions/13777666/no-rollback-for-constraintviolationexception-in-transactional-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!