接下来测试事务传播属性设置为NOT_SUPPORTED
Service层
Service层主要设置如下,其中还插入了REQUIRED作为比较。
package Service; import javax.annotation.Resource; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import DAO.EmpDAO;
import Entity.EMP; @Service("service1")
public class EMPService1Impl implements EMPService1{ @Resource(name="empDAO")
EmpDAO dao; @Transactional(propagation=Propagation.NOT_SUPPORTED)
public void addEmp1(EMP emp) {
dao.addEMP1(emp);
} }
package Service; import javax.annotation.Resource; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import DAO.EmpDAO;
import Entity.EMP; @Service("service2")
public class EMPService2Impl implements EMPService2{ @Resource(name="empDAO")
EmpDAO dao; @Transactional(propagation=Propagation.REQUIRED)
public void addEmp2(EMP emp) {
dao.addEMP2(emp);
} @Transactional(propagation=Propagation.NOT_SUPPORTED)
public void addEmp2WithException(EMP emp) {
dao.addEMP2(emp);
throw new RuntimeException();
} }
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;
@Component("noteSupportedTest")
public class NotSupportedTest {
@Resource(name="service1")
EMPService1 service1;
@Resource(name="service2")
EMPService2 service2;
/**
* 外层方法没有事务,但是抛出异常
* @param emp1
* @param emp2
*/
public void testNotSupportWithoutTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法没有事务,内层方法抛出异常
* @param emp1
* @param emp2
*/
public void testNotSupportWithoutTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,并且抛出异常
* @param emp1
* @param emp2
*/
@Transactional
public void testNotSupportWithTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);//not_supported
service2.addEmp2(emp2);//required
throw new RuntimeException();
}
/**
* 外层方法有事务,并且内存方法抛出异常
* @param emp1
* @param emp2
* @param emp3
*/
@Transactional
public void testNotSupportWithTransaction2(EMP emp1,EMP emp2, EMP emp3) {
service1.addEmp1(emp1);//not_supported
service2.addEmp2(emp2);//required
service2.addEmp2WithException(emp3);//not_supported
}
}
测试代码
package TestCase; import org.junit.Test; import Entity.EMP;
import LayerT.NotSupportedTest; public class notSupportedTestCase extends baseTest{
@Test
public void test1() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testNotSupportWithoutTransaction1(emp1, emp2);
}
@Test
public void test2() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testNotSupportWithoutTransaction2(emp1, emp2);
}
@Test
public void test3() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testNotSupportWithTransaction1(emp1, emp2);
}
@Test
public void test4() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
EMP emp3=new EMP("王五",22);
T1.testNotSupportWithTransaction2(emp1, emp2, emp3);
}
}
测试结果
(1)外层方法没有事务
test1 | 张三插入,李四插入 |
test2 | 张三插入,李四插入 |
结论:当外层方法没有事务时,内层方法按照非事务方式执行。
(2)外层方法有事务
test3 | 张三插入,李四未插入 |
test4 | 张三插入,李四未插入,王五插入 |
结论:当外层方法事务传播属性设置为默认,只有声明REQUIRED的那个方法会受外层方法的影响,导致李四未插入,但是内层方法事务声明为NOT_SUPPORTED的方法,不论内层还是外层方法有异常,都不开启事务,出错也提交事务。