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

时间:2022-11-16 15:56:10

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

Service层

Service层将方法的事务传播属性设置为SUPPORTS

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;
/**
* 测试Supports
* @author yangchaolin
*
*/
@Component("supportTest")
public class SupportedTest {
@Resource(name="service1")
EMPService1 service1;
@Resource(name="service2")
EMPService2 service2; /**
* 外层方法没有事务,但是抛出异常
* @param emp1
* @param emp2
*/
public void testSupportsWithoutTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法没有事务,内层方法抛出异常
* @param emp1
* @param emp2
*/
public void testSupportsWithoutTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,并抛出异常
* @param emp1
* @param emp2
*/
@Transactional
public void testSupportsWithTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法有事务,内层方法抛出异常
* @param emp1
* @param emp2
*/
@Transactional
public void testSupportsWithTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,内层方法抛出异常被捕获
* @param emp1
* @param emp2
*/
@Transactional
public void testSupportsWithTransaction3(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
try {
service2.addEmp2WithException(emp2);
}catch(Exception e) {
System.out.println("回滚");
}
}
}

测试代码

 package TestCase;

 import org.junit.Test;

 import Entity.EMP;
import LayerT.SupportedTest; public class supportsTestCase extends baseTest{
@Test
public void test1() {
SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testSupportsWithoutTransaction1(emp1, emp2);
}
@Test
public void test2() {
SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testSupportsWithoutTransaction2(emp1, emp2);
}
@Test
public void test3() {
SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testSupportsWithTransaction1(emp1, emp2);
}
@Test
public void test4() {
SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testSupportsWithTransaction2(emp1, emp2);
}
@Test
public void test5() {
SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testSupportsWithTransaction3(emp1, emp2);
}
}

测试结果

(1)外层方法没有事务

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

结论:当内层方法事务传播属性设置为SUPPORTS时,在外层方法没有声明事务的情况下,按照非事务方式执行,所以test2方法执行后依然能插入李四。

(2)外层方法有事务

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

结论:当外层方法支持事务传播时,内层事务加入到外层事务,绑定到一起,只要其中一个发生异常,不管内层方法执行时是否捕获异常,整个事务都需要回滚,这点类似Required。

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