捕获异常的事务回滚不起作用

时间:2019-01-15 11:49:57

标签: spring spring-boot jpa spring-data-jpa transactional

当我在catch块中抛出异常时,@ Transactional不会回滚事务。

@Transactional(rollbackFor = MyException.class)
public void testTransactional2() throws Exception {
    try {
        dao1.save(entity1);
        dao2.save(entity2);
        arrayList.get(999999); // intentionally cause an exception
    } catch (IndexOutOfBoundsException e) {
        throw new MyException(ErrorCode.UNABLE_TO_INSERT, e);
    }
}

dao1.save()dao2.save()本身都带有@Transactional注释。

当我检查数据库时,我看到两个实体都保留了。

1 个答案:

答案 0 :(得分:-1)

注意:默认情况下,@ Transactional设置为仅在引发未经检查的异常时回滚。

与此同时删除您的throws子句,而是抛出MyException。

相关问题