【问题标题】:Spring nested transaction handling and nested propogationSpring嵌套事务​​处理和嵌套传播
【发布时间】:2012-10-20 14:04:53
【问题描述】:

我有一个下面的场景,我想了解 Spring 中的嵌套事务处理以及传播。我实际上已经阅读了足够多的内容,但仍然不清楚一些事实。

public class ServiceImpl {
@Autowired 
public AnotherService anotherService;

@Transactional // by default it is PROPOGATION_REQUIRED
public void insert (){
    anotherService.anotherInsert();
}
}

public class AnotherServiceImpl {

@Transactional(propagation = Propagation.NESTED)
public void anotherInsert() {
    insertSomeTestData();
}

private void insertSomeTestData() {
    // call insert some test data recursively
    // insert trasaction
    insertSomeTestData();
}
}

在这种情况下,anotherInsert 将提交嵌套数据并回滚某些数据,这是否会影响默认情况下的外部事务PROPOGATION_REQUIRED 另外不清楚的一点是,如果事务嵌套,会发生什么?他们在这种情况下开始新的?

【问题讨论】:

    标签: spring spring-aop spring-transactions spring-annotations


    【解决方案1】:

    这里是嵌套的描述

    “如果当前事务存在,则在嵌套事务中执行,否则它的行为类似于 PROPAGATION_REQUIRED else”

    在上述场景中,由于存在一个当前事务,它将继续在嵌套事务下执行,并且不会创建新事务。

    如果我错了,请纠正我。谢谢

    【讨论】:

    • “AnotherService.anotherInsert”调用将创建一个嵌套事务,因为 ServiceImpl.insert 中已经存在一个现有事务。
    【解决方案2】:

    “AnotherService.anotherInsert”调用将在 ServiceImpl.insert 中已存在的逻辑事务下创建嵌套逻辑事务。

    嵌套事务的回滚不会像 REQUIRES_NEW 行为那样回滚主事务。

    规格:

    PROPAGATION_NESTED 使用单个物理事务和多个 它可以回滚到的保存点。这种部分回滚允许 内部事务范围触发其范围的回滚,使用 外部交易能够继续物理交易 尽管已经回滚了一些操作。这个设置是 通常映射到 JDBC 保存点,因此仅适用于 JDBC 资源交易。请参阅 Spring 的 DataSourceTransactionManager。

    但是,您应该知道,嵌套事务基于 JDBC 保存点,使用单个物理事务。这意味着您必须使用 JDBC 数据源和以下事务管理器才能使其工作:

    org.springframework.jdbc.datasource.DataSourceTransactionManager

    参照。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#tx-propagation-nested

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2019-07-12
      • 2016-09-10
      • 2017-12-24
      • 1970-01-01
      • 2021-11-17
      • 2021-04-25
      • 1970-01-01
      • 2011-11-01
      相关资源
      最近更新 更多