具体参照https://www.cnblogs.com/weixiaotao/p/10538158.html
注:之前在测试的时候,一直未得到预想的结果。
单元测试:
@Test
@Transactional(propagation = Propagation.REQUIRED)
public void test3() throws Exception {
Emp emp3 = new Emp();
emp3.setEmpno("105");
emp3.setEname("zhaoliu");
emp3.setSal(1111D);
insertService.insertEmp(emp3);
}
@Transactional(propagation = Propagation.REQUIRED)
public void insertEmp(Emp emp){
mapper.insertSelective(emp);
}
执行的时候,没有报错,但是数据库也没有数据。把 @Transactional(propagation = Propagation.REQUIRED) 事务去掉居然又可以了。后面想到应该是在service层进行事务设置,单元测试这边相当于调用的controller层,不应该设置事务。
@Transactional(propagation = Propagation.REQUIRED)
public void insertEmp(Emp emp){
mapper.insertSelective(emp);
}
@Transactional(propagation = Propagation.REQUIRED)
public void invokeSave(Emp emp){
insertEmp(emp);
}
@Test
public void test2() throws Exception {
Emp emp3 = new Emp();
emp3.setEmpno("105");
emp3.setEname("zhaoliu");
emp3.setSal(1111D);
insertService.invokeSave(emp3);
}
果然执行成功,数据库成功插入数据。
验证,Propagation.REQUIRES_NEW
@Service
public class EmpService {
@Autowired
private EmpMapper empMapper;
@Transactional(propagation = Propagation.REQUIRED)
public void saveEmpR(Emp emp){
empMapper.insertSelective(emp);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveEmp(Emp emp){
empMapper.insertSelective(emp);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveEmpExpection(Emp emp){
empMapper.insertSelective(emp);
throw new RuntimeException("saveEmpExpection 执行出现异常");
}
@Transactional(propagation = Propagation.REQUIRED)
public void save(){
Emp emp3 = new Emp();
emp3.setEmpno("103");
emp3.setEname("wangwu");
saveEmpR(emp3);
//本来101应该插入成功,但是没插入
Emp emp = new Emp();
emp.setEmpno("101");
emp.setEname("zhangsan");
saveEmp(emp);
Emp emp2 = new Emp();
emp2.setEmpno("102");
emp2.setEname("lisi");
saveEmpExpection(emp2);
}
}
@Test
void test1() throws Exception {
empService.save();
}
预计 101 能够成功插入,但是没有成功。
需要再写一个service,然后调用EmpService才有效果
public class InsertService {
@Autowired
private EmpMapper mapper;
@Autowired
private EmpService empService;
@Transactional(propagation = Propagation.REQUIRED)
public void insertEmp(Emp emp){
mapper.insertSelective(emp);
}
@Transactional(propagation = Propagation.REQUIRED)
public void invokeSave(Emp emp){
insertEmp(emp);
}
//执行结果: 103 和 101插入成功,102插入失败
@Transactional(propagation = Propagation.REQUIRED)
public void save1(){
Emp emp3 = new Emp();
emp3.setEmpno("103");
emp3.setEname("wangwu");
empService.saveEmpR(emp3);
Emp emp = new Emp();
emp.setEmpno("101");
emp.setEname("zhangsan");
empService.saveEmp(emp);
Emp emp2 = new Emp();
emp2.setEmpno("102");
emp2.setEname("lisi");
//这边捕获到Propagation.REQUIRES_NEW的异常,所以 103能够插入成功
try{
empService.saveEmpExpection(emp2);
}catch (Exception e){
System.out.println(e.getMessage());
}
}
//执行结果:101插入成功,103和102插入失败
@Transactional(propagation = Propagation.REQUIRED)
public void save2(){
Emp emp3 = new Emp();
emp3.setEmpno("103");
emp3.setEname("wangwu");
empService.saveEmpR(emp3);
Emp emp = new Emp();
emp.setEmpno("101");
emp.setEname("zhangsan");
empService.saveEmp(emp);
Emp emp2 = new Emp();
emp2.setEmpno("102");
emp2.setEname("lisi");
//因为该处执行异常,所以103插入失败
empService.saveEmpExpection(emp2);
}
}