【问题标题】:Propagation_requires_new rollbacks suspended transactionPropagation_requires_new 回滚暂停的事务
【发布时间】:2015-10-16 05:49:09
【问题描述】:

我有以下代码

@服务 公共类员工服务{ 员工服务员工服务; //测试不同的行为

@PersistenceContext
EntityManager entityManager;

@Transactional(propagation = Propagation.REQUIRED)
public void requiredPropagationMethod(){

    System.out.println("EmployeeService.requiredPropagationMethod");
    Employee e = new Employee();
    e.setEmpName("required propagation method employee");
    entityManager.persist(e);
    employeeService.requiresNewPropagationMethod(); //creates a new transaction and suspends old one
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void requiresNewPropagationMethod(){
    System.out.println("EmployeeService.requiresNewPropagationMethod");
    Employee e = new Employee();
    e.setEmpName("requires new propagation method employee");
    entityManager.persist(e);
    throw new RuntimeException("Roll back requires new method");
}

}

在执行此代码后,我期待 Employee 表中有名为“必需传播方法员工”的员工,但它不存在,这意味着两个事务都已回滚。但预期的行为是不应该的。我需要知道为什么会这样?

【问题讨论】:

    标签: spring transactions propagation


    【解决方案1】:

    实际上,当从一个方法调用另一个方法时,第二个不会被代理拦截。在此示例中,您通过传递 Propagation.REQUIRES_NEW 因为您是从另一个方法调用它。第二个无法启动新事务。它使用与第一种方法相同的事务。因此,当发生异常时,第一个事务正在回滚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 2017-11-12
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多