在准备好前期的项目搭建后,接下来就一个个的测试,首先测试事务传播的Required

Service层两个实现类

Service层两个实现类,只是更换了方法事务传播的属性,其他都一样,后续测试也只修改传播的属性即可,这里只列一次便于理解。

 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.REQUIRED)
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.REQUIRED)
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.Propagation;
import org.springframework.transaction.annotation.Transactional; import Entity.EMP;
import Service.EMPService1;
import Service.EMPService2; /**
* 测试Required
* @author yangchaolin
*
*/
@Component("requiredTest")
public class RequiredTest {
@Resource(name="service1")
EMPService1 service1;
@Resource(name="service2")
EMPService2 service2; /**
* 外层方法没有事务,但是抛出异常
* @param emp1
* @param emp2
*/
public void testRequiredWithoutTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法没有事务,内层有方法抛出异常
* @param emp1
* @param emp2
*/
public void testRequiredWithoutTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,但是抛出异常
* @param emp1
* @param emp2
*/
@Transactional(propagation=Propagation.REQUIRED)
public void testRequiredWithTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法有事务,内层有方法抛出异常
* @param emp1
* @param emp2
*/
@Transactional(propagation=Propagation.REQUIRED)
public void testRequiredWithTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,内存方法抛出的异常被捕获
* @param emp1
* @param emp2
*/
@Transactional(propagation=Propagation.REQUIRED)
public void testRequiredWithTransaction3(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
try {
service2.addEmp2WithException(emp2);
}catch(Exception e) {
System.out.println("回滚");
}
} }

测试代码

其中baseTest为父类,里面读取Spring-MVC.xml和Spring-Mybatis配置文件后初始化了ApplicationContext,继承它后后续其他事务传播属性的测试就不需要重复写这一部分代码。

 package TestCase;

 import org.junit.Test;

 import Entity.EMP;
import LayerT.RequiredTest; public class requiredTestCase extends baseTest{
@Test
public void test1() {
RequiredTest T1=ac.getBean("requiredTest",RequiredTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testRequiredWithoutTransaction1(emp1, emp2);
}
@Test
public void test2() {
RequiredTest T1=ac.getBean("requiredTest",RequiredTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testRequiredWithoutTransaction2(emp1, emp2);
}
@Test
public void test3() {
RequiredTest T1=ac.getBean("requiredTest",RequiredTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testRequiredWithTransaction1(emp1, emp2);
}
@Test
public void test4() {
RequiredTest T1=ac.getBean("requiredTest",RequiredTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testRequiredWithTransaction2(emp1, emp2);
}
@Test
public void test5() {
RequiredTest T1=ac.getBean("requiredTest",RequiredTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testRequiredWithTransaction3(emp1, emp2);
}
}

(1)外层方法没有事务测试结果

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

结论:在外层方法没有开启事务的情况下,传播属性为Required的方法,会各自执行事务,互不干扰,当方法出现异常时,会出现回滚,如李四就未插入。

(2)外层方法有事务测试结果

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

结论:在外层方法开启事务,并且传播属性为默认Required的时候,内层方法与外层方法事务传播属性一样,会将事务绑定在一起,不论是外层还是内层方法出现异常,都会导致数据回滚,即使将内层抛出异常的方法捕获也没有用。

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

云笔记项目-Spring事务学习-传播Requried的更多相关文章

  1. 云笔记项目-Spring事务学习-传播NOT_SUPPORTED

    接下来测试事务传播属性设置为NOT_SUPPORTED Service层 Service层主要设置如下,其中还插入了REQUIRED作为比较. package Service; import java ...

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

    接下来测试事务传播属性SUPPORTS Service层 Service层将方法的事务传播属性设置为SUPPORTS LayerT层代码 package LayerT; import javax.an ...

  3. 云笔记项目-Spring事务学习-传播NEVER

    接下来测试事务传播属性NEVER Service层 Service层中设置事务传播属性都为NEVER. LayerT层代码 package LayerT; import javax.annotatio ...

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

    接下来测试事务传播属性MANDATORY Service层 所有Service层实现类都设置事务传播属性为MANDATORY. LayerT层代码 package LayerT; import jav ...

  5. 云笔记项目-Spring事务学习-传播NESTED

    接下来测试事务传播属性NESTED Service层 Service层方法事务传播属性都设置为NESTED. LayerT层代码 package LayerT; import javax.annota ...

  6. 云笔记项目-Spring事务学习-传播REQUIRES_NEW

    接下来测试事务传播的REQUIRES_NEW. Service层 Service层代码在这里不展示了,主要将EMPService1Impl类中的方法事务传播属性设置为REQUIRED,EMPServi ...

  7. 云笔记项目-Spring事务学习_测试准备

    在做云笔记项目的过程中,顺便简单的学习了Spring的事务概念,业务以如果添加笔记,则增加用户星星数目作为例子,引入了事务的概念.类似注册送积分之类的,云笔记项目以增加笔记就送星星来说明事务.具体在添 ...

  8. Spring事务的传播行为 @Transactional(转)

    Spring事务的传播行为 在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例 ...

  9. Spring事务的传播行为 @Transactional

    Spring事务的传播行为http://blog.csdn.net/cuker919/article/details/5957209 在service类前加上@Transactional,声明这个se ...

随机推荐

  1. webAPP如何实现移动端拍照上传(Vue组件示例)?

    摘要:使用HTML5编写移动Web应用,主要是为了尝试一下“一套代码多处运行”,一个webapp几乎可以不加修改的运行在PC/Android/iOS等上面运行.但是写到现在觉得虽然这种方式弊大于利,不 ...

  2. php7.2 sqlsrv 扩展 ubuntu Homestead centOs

    PHP 7.2.9-1+ubuntu18.04.1 安装 sqlsrv 扩展 参考文章,感谢作者(建议先看,不看也可以) https://serverpilot.io/docs/how-to-inst ...

  3. Delphi编译选项

    编译选项的设置,称为“开关指令”,其中大部分值为布尔类型 一.代码生成(Code generation)1.Optimization  优化代码,默认true2.Stack frames  生成过程/ ...

  4. bootice-diskinfo参数

    http://bbs.wuyou.net/forum.php?mod=redirect&goto=findpost&ptid=57675&pid=3023573&fro ...

  5. 报错:Failed on local exception: Host Details : local host is: "master/192.168.52.26"; dest

    报错现象 Failed on local exception: com.google.protobuf.InvalidProtocolBufferException: Protocol message ...

  6. CentOS7的内核优化

    修改内核配置文件 vim /etc/sysctl.conf 刷新配置文件 sysctl -p 关ipv6 net.ipv6.conf.all.disable_ipv6 = net.ipv6.conf. ...

  7. 2019年1月16日22:50:28 白糖SR1905

    很好的机会,只拿了点皮毛,如果说都是因为上班时间不充裕那是给自己找借口,最主要原因没别的:思维不清,策略不明- 这里的入场初衷是周线区间下沿,日线向下脱离中枢失败后回拉一笔,那么这一单的做法就应该很明 ...

  8. .net core下载文件

    上传的文件是在wwwroot下  通过保存的路径跟文件名称完成下载 public IActionResult DownloadFile() { var filePath = "/Upload ...

  9. python-pytest学习

    一:pytest基于unittest之上的单元测试框架1.自动发现测试模块和测试方法:2.断言使用assert+表达式即可:3.可以设置会话级.模块级.类级.函数级的fixtures :数据准备+清理 ...

  10. pl-svo在ROS下运行笔记

    一.程序更改的思路(参考svo_ros的做法): 1.在ROS下将pl-svo链接成库需要更改相应的CMakeLists.txt文件,添加package.xml文件: 2.注册一个ROS节点使用svo ...