云笔记项目-Spring事务学习-传播MANDATORY

时间:2022-05-27 10:16:40

接下来测试事务传播属性MANDATORY

Service层

所有Service层实现类都设置事务传播属性为MANDATORY。

LayerT层代码

 package LayerT;

 import javax.annotation.Resource;

 import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import Entity.EMP;
import Service.EMPService1;
import Service.EMPService2; /**
* 测试Mandatory
* @author yangchaolin
*
*/
@Component("mandatoryTest")
public class MandatoryTest {
@Resource(name="service1")
EMPService1 service1;
@Resource(name="service2")
EMPService2 service2;
/**
* 外层方法没有事务,但是抛出异常
* @param emp1
* @param emp2
*/
public void testMandatoryWithoutTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法没有事务,内层方法抛出异常
* @param emp1
* @param emp2
*/
public void testMandatoryWithoutTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,并且抛出异常
* @param emp1
* @param emp2
*/
@Transactional
public void testMandatoryWithTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法有事务,内层方法抛出异常
* @param emp1
* @param emp2
*/
@Transactional
public void testMandatoryWithTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,内层方法抛出异常被捕获
* @param emp1
* @param emp2
*/
@Transactional
public void testMandatoryWithTransaction3(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
try {
service2.addEmp2WithException(emp2);
}catch(Exception e) {
System.out.println("回滚");
}
}
/**
* 外层方法有事务,没有异常发生
* @param emp1
* @param emp2
*/
@Transactional
public void testMandatoryWithTransaction4(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
}
}

测试代码

 package TestCase;

 import org.junit.Test;

 import Entity.EMP;
import LayerT.MandatoryTest; public class mandatoryTestCase extends baseTest{
@Test
public void test1() {
MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testMandatoryWithoutTransaction1(emp1, emp2);
}
@Test
public void test2() {
MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testMandatoryWithoutTransaction2(emp1, emp2);
}
@Test
public void test3() {
MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testMandatoryWithTransaction1(emp1, emp2);
}
@Test
public void test4() {
MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testMandatoryWithTransaction2(emp1, emp2);
}
@Test
public void test5() {
MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testMandatoryWithTransaction3(emp1, emp2);
}
@Test
public void test6() {
MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testMandatoryWithTransaction4(emp1, emp2);
}
}

测试结果

(1)外层方法没有事务

test1 张三未插入,李四未插入
test2 张三未插入,李四未插入

测试报错内容为:"No existing transaction found for transaction marked with propagation 'mandatory' "。

结论:外层方法没有事务,内层方法事务传播属性为MANDATROY时,将无法插入,并执行报上述错误,提示找到不到已有事务,即找不到外层方法中的事务。

(2)外层方法有默认事务属性

test3 张三未插入,李四未插入
test4 张三未插入,李四未插入
test5 张三未插入,李四未插入
test6 张三插入,李四插入

结论:只有外层方法有事务的情况下,才能执行内层声明事务传播属性为MANDATORY的方法,否则将报错。当外层方法添加事务后,内层或者外层方法随便一个出异常,都会导致整体事务回滚,只有都没有异常时才能正常插入数据。

参考博文:https://segmentfault.com/a/1190000013341344