【发布时间】: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