云笔记项目-Spring事务学习-传播MANDATORY
接下来测试事务传播属性MANDATORY
Service层
所有Service层实现类都设置事务传播属性为MANDATORY。
LayerT层代码
1 package LayerT; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Component; 6 import org.springframework.transaction.annotation.Transactional; 7 8 import Entity.EMP; 9 import Service.EMPService1; 10 import Service.EMPService2; 11 12 /** 13 * 测试Mandatory 14 * @author yangchaolin 15 * 16 */ 17 @Component("mandatoryTest") 18 public class MandatoryTest { 19 @Resource(name="service1") 20 EMPService1 service1; 21 @Resource(name="service2") 22 EMPService2 service2; 23 /** 24 * 外层方法没有事务,但是抛出异常 25 * @param emp1 26 * @param emp2 27 */ 28 public void testMandatoryWithoutTransaction1(EMP emp1,EMP emp2) { 29 service1.addEmp1(emp1); 30 service2.addEmp2(emp2); 31 throw new RuntimeException(); 32 } 33 /** 34 * 外层方法没有事务,内层方法抛出异常 35 * @param emp1 36 * @param emp2 37 */ 38 public void testMandatoryWithoutTransaction2(EMP emp1,EMP emp2) { 39 service1.addEmp1(emp1); 40 service2.addEmp2WithException(emp2); 41 } 42 /** 43 * 外层方法有事务,并且抛出异常 44 * @param emp1 45 * @param emp2 46 */ 47 @Transactional 48 public void testMandatoryWithTransaction1(EMP emp1,EMP emp2) { 49 service1.addEmp1(emp1); 50 service2.addEmp2(emp2); 51 throw new RuntimeException(); 52 } 53 /** 54 * 外层方法有事务,内层方法抛出异常 55 * @param emp1 56 * @param emp2 57 */ 58 @Transactional 59 public void testMandatoryWithTransaction2(EMP emp1,EMP emp2) { 60 service1.addEmp1(emp1); 61 service2.addEmp2WithException(emp2); 62 } 63 /** 64 * 外层方法有事务,内层方法抛出异常被捕获 65 * @param emp1 66 * @param emp2 67 */ 68 @Transactional 69 public void testMandatoryWithTransaction3(EMP emp1,EMP emp2) { 70 service1.addEmp1(emp1); 71 try { 72 service2.addEmp2WithException(emp2); 73 }catch(Exception e) { 74 System.out.println("回滚"); 75 } 76 } 77 /** 78 * 外层方法有事务,没有异常发生 79 * @param emp1 80 * @param emp2 81 */ 82 @Transactional 83 public void testMandatoryWithTransaction4(EMP emp1,EMP emp2) { 84 service1.addEmp1(emp1); 85 service2.addEmp2(emp2); 86 } 87 }
测试代码
1 package TestCase; 2 3 import org.junit.Test; 4 5 import Entity.EMP; 6 import LayerT.MandatoryTest; 7 8 public class mandatoryTestCase extends baseTest{ 9 @Test 10 public void test1() { 11 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 12 EMP emp1=new EMP("张三",18); 13 EMP emp2=new EMP("李四",28); 14 T1.testMandatoryWithoutTransaction1(emp1, emp2); 15 } 16 @Test 17 public void test2() { 18 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 19 EMP emp1=new EMP("张三",18); 20 EMP emp2=new EMP("李四",28); 21 T1.testMandatoryWithoutTransaction2(emp1, emp2); 22 } 23 @Test 24 public void test3() { 25 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 26 EMP emp1=new EMP("张三",18); 27 EMP emp2=new EMP("李四",28); 28 T1.testMandatoryWithTransaction1(emp1, emp2); 29 } 30 @Test 31 public void test4() { 32 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 33 EMP emp1=new EMP("张三",18); 34 EMP emp2=new EMP("李四",28); 35 T1.testMandatoryWithTransaction2(emp1, emp2); 36 } 37 @Test 38 public void test5() { 39 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 40 EMP emp1=new EMP("张三",18); 41 EMP emp2=new EMP("李四",28); 42 T1.testMandatoryWithTransaction3(emp1, emp2); 43 } 44 @Test 45 public void test6() { 46 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 47 EMP emp1=new EMP("张三",18); 48 EMP emp2=new EMP("李四",28); 49 T1.testMandatoryWithTransaction4(emp1, emp2); 50 } 51 }
测试结果
(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
转载于:https://www.cnblogs.com/youngchaolin/p/10629795.html
云笔记项目-Spring事务学习-传播MANDATORY相关推荐
- 面试官:Spring事务的传播行为有几种?
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:程序员入职国企,1周上班5小时,晒出薪资感叹:腾讯当CEO也不去个人原创+1博客:点击前往,查看更多 在Spri ...
- Spring事务的传播机制
spring事务的传播机制 1.spring事务 指封装在数据库事务之上的一种事务处理机制.其管理方法有两种,分别是编程式事务以及声明式事务.一般我们使用@Transactional进行声明式事务. ...
- spring事务的传播
1.spring事务的传播 为什么会有传播机制 spring对事务的控制,是使用aop切面实现的 场景一:A调用B,都有事务,B异常,让B提交还是两个一起回滚 场景二:A调用B,A有事务,是够让B加入 ...
- Spring事务的传播行为
Spring事务的传播行为 Spring事务的传播行为有七种,对应着事务定义接口TransactionDefinition中的七种状态.PROPAGATION_REQUIRED.PROPAGATION ...
- spring手动控制事务开启_“上帝视角”图解Spring事务的传播机制原理
转载:https://mp.weixin.qq.com/s/odP1DKgRtXsCcAKxwGahug 数据库事务的"抓手" 数据库的事务功能已经由数据库自身实现,它留给用户的就 ...
- 数据库事务原理详解-Spring 事务的传播属性
所谓spring 事务的传播属性,就是定义在存在多个事务同时存在的时候,spring 应该如何处理这些事务的行为.这些属性在TransactionDefinition 中定义,具体常量的解释见下表: ...
- Spring 事务的传播属性
Spring 事务的传播属性 事务的传播属性: 1.PROPAGATION_REQUIRED * :如果不存在事务则新建事务,若存在事务则加入事务,默认是这个 2.PROPAGATION_SUPPOR ...
- Spring事务的传播行为案例分析
简介:网上关于Spring事务传播性以及隔离型的文章漫天盖地,还有不负责任的直接复制名词意思,文章虽然很多却是看的云里雾里,我们今天将给出案例分别和大家一起学习. 网上关于Spring事务传播性以及隔 ...
- 事务隔离级别和传播行为_?Spring事务的传播行为案例分析
简介:网上关于Spring事务传播性以及隔离型的文章漫天盖地,还有不负责任的直接复制名词意思,文章虽然很多却是看的云里雾里,我们今天将给出案例分别和大家一起学习. 网上关于Spring事务传播性以及隔 ...
最新文章
- 计算机应用能力测试攻略,计算机应用能力测试题(一).doc
- YbtOJ-相似子串【SA,RMQ,二分】
- 发掘Apache Camel的力量
- 4.3/4.4 磁盘分区
- linux服务器搭建教程c,Linux服务器上搭建web项目环境
- 数据类型以及数据类型的转换---防止忘记
- python显示函数图像_python – 显示存储在函数中的图像
- iOS:选择器控件UIPickerView的详解和演示
- 速度提升3000倍,微软FastNeRF首次实现200FPS高保真神经渲染
- PHP树形结构数据增加每层的级别
- [置顶] cocos2d-x2.2.5走四棋儿源码“开源”
- 青蓝电影质感LR预设达芬奇/PS/PR/LUT人像lightroom胶片调色滤镜插件
- 28款数据恢复软件分类介绍
- linux 内核书籍记录
- 小团团云上城在哪个服务器,云上城之歌小团团
- 分布式 | DBLE docker 部署遇到的简单问题修复过程
- 将esx虚拟机从一台服务器迁移,虚拟化应用支招:ESX VtoP迁移实战
- 蓝桥杯——种植园问题(prev54)
- 15-top命令详解
- 隐藏控制台应用程序的窗口
热门文章
- 百度网盘javascript加速视频播放速度
- 创建CSS和处理的一些方法
- Linux下oracle数据库spfile参数配置文件丢失问题解决,“ORA-32001: write to SPFILE requested but no SPFILE is in use“问题处理
- Java 技术篇-linux系统下安装jdk、设置java环境变量实例演示
- Python 用smtplib库发邮件报错:[WinError 10061] 由于目标计算机积极拒绝,无法连接。解决办法
- 【嵌入式干货】利用二分法定位Flash存有数据(非FF)的地址
- CTFshow 命令执行 web59
- 使用numba要注意的越界问题
- 对整个矩阵元素进行计算:最大数、最小数、排序
- 平方剩余(二次剩余)