首页 > 解决方案 > Spring事务行为

问题描述

似乎当具有 NESTED 传播的事务弹簧方法调用另一个具有传播 REQUIRED 的事务方法时,内部事务可以强制回滚外部逻辑事务。任何人都可以确认吗?

我想处理 RuntimeException 而不是回滚外部事务,例如:

@Transactional
class A {

    @Autowired
    B b;

    @Autowired
    C c;

    void methodA() { // outer transaction, this should not be rollback but currently getting UnexpectedRollbackException
        b.methodB(() -> c.methodC());
    }
}

@Transactional(propagation = Propagation.NESTED)
class B {

    void methodB(Runnable action) { // inner nested transaction
        try{
            action.run();
        } catch (Exception e){
           // nothing
        }
    }
}

@Transactional
class C {
    void methodC() { // inner required transaction
        throw new RuntimeException();
    }
}

标签: javaspring-transactions

解决方案


为什么不呢?NESTED如果存在,则传播在当前交易中开始交易,REQUIRED否则行为类似。javadocs 状态:

/**
 * Support a current transaction; create a new one if none exists.
 * Analogous to the EJB transaction attribute of the same name.
 * <p>This is typically the default setting of a transaction definition,
 * and typically defines a transaction synchronization scope.
 */
int PROPAGATION_REQUIRED = 0;

/**
 * Execute within a nested transaction if a current transaction exists,
 * behave like {@link #PROPAGATION_REQUIRED} else. There is no analogous
 * feature in EJB.
 * <p><b>NOTE:</b> Actual creation of a nested transaction will only work on
 * specific transaction managers. Out of the box, this only applies to the JDBC
 * {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
 * when working on a JDBC 3.0 driver. Some JTA providers might support
 * nested transactions as well.
 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
 */
int PROPAGATION_NESTED = 6;

值得注意的是,NESTED仅当您的 JDBC 驱动程序支持保存点时才真正受支持。这表示:

没有现有交易
A(嵌套)
B(必填)

会有以下行为:

begin; -- called prior to A but in A's 
A.doSomething();
B.doSomethingThatCausesException()
rollback;

现有交易
A(嵌套)
B(必填)

会有以下行为:

begin; -- called outside of the scope of A
savepoint A_savepoint
A.doSomething();
B.doSomethingThatCausesException();
rollback A_savepoint;

如果您的 JDBC 驱动程序支持嵌套事务。否则,它将表现得像第一个场景。另请参阅答案。

也就是说,我相信保存点比它们的价值更麻烦,如果您以原子方式处理任何数据库操作,您将为自己节省很多潜在的麻烦。


推荐阅读