【问题标题】:Spring transaction: requires_new beharivour春季交易:requires_new 行为
【发布时间】:2014-04-08 04:47:12
【问题描述】:

可能是我误解了 Spring Requires_new 行为。这是我的代码:

@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public void outterMethod() throws Exception{
    innerMethod1();
    innerMethod2();         
}
@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRES_NEW)
public void innerMethod1() throws Exception{
    testService.insert(new Testbo("test-2", new Date()));
}

@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRES_NEW)
public void innerMethod2() throws Exception{
    testService.insert(new Testbo("test-2", new Date()));
    throw new Exception();
}

当innnerMethod2 抛出异常时,我认为innerMethod1 仍然能够提交。但是所有外部和内部事务都会回滚。我在这里错了什么?当 innerMethod2 回滚时,我该如何提交 innerMethod1?

【问题讨论】:

    标签: spring transactions behavior


    【解决方案1】:

    尽管您已经正确理解了 Propagation.REQUIRES_NEW 的行为,但您偶然发现了对 Spring 的事务行为的常见误解。

    为了应用事务语义(即方法的注解有任何效果),需要从类外部调用方法。从类内部调用带有 transactional 注释的方法对事务处理绝对没有影响(因为 Spring 生成的包含事务代码的代理类不起作用)。

    在您的示例中,innerMethod2 可能使用 @Transactional 进行了注释,但由于它是从 outterMethod 调用的,因此未处理注释。

    查看 this 部分 Spring 文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2019-02-14
      • 2014-05-28
      • 2012-08-10
      • 1970-01-01
      • 2011-08-27
      相关资源
      最近更新 更多