云笔记项目-Spring事务学习-传播MANDATORY
接下来测试事务传播属性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
云笔记项目-Spring事务学习-传播MANDATORY的更多相关文章
- 云笔记项目-Spring事务学习-传播Requried
在准备好前期的项目搭建后,接下来就一个个的测试,首先测试事务传播的Required Service层两个实现类 Service层两个实现类,只是更换了方法事务传播的属性,其他都一样,后续测试也只修改传 ...
- 云笔记项目-Spring事务学习-传播NOT_SUPPORTED
接下来测试事务传播属性设置为NOT_SUPPORTED Service层 Service层主要设置如下,其中还插入了REQUIRED作为比较. package Service; import java ...
- 云笔记项目-Spring事务学习-传播SUPPORTS
接下来测试事务传播属性SUPPORTS Service层 Service层将方法的事务传播属性设置为SUPPORTS LayerT层代码 package LayerT; import javax.an ...
- 云笔记项目-Spring事务学习-传播NEVER
接下来测试事务传播属性NEVER Service层 Service层中设置事务传播属性都为NEVER. LayerT层代码 package LayerT; import javax.annotatio ...
- 云笔记项目-Spring事务学习-传播NESTED
接下来测试事务传播属性NESTED Service层 Service层方法事务传播属性都设置为NESTED. LayerT层代码 package LayerT; import javax.annota ...
- 云笔记项目-Spring事务学习-传播REQUIRES_NEW
接下来测试事务传播的REQUIRES_NEW. Service层 Service层代码在这里不展示了,主要将EMPService1Impl类中的方法事务传播属性设置为REQUIRED,EMPServi ...
- 云笔记项目-Spring事务学习_测试准备
在做云笔记项目的过程中,顺便简单的学习了Spring的事务概念,业务以如果添加笔记,则增加用户星星数目作为例子,引入了事务的概念.类似注册送积分之类的,云笔记项目以增加笔记就送星星来说明事务.具体在添 ...
- Spring事务的传播行为 @Transactional
Spring事务的传播行为http://blog.csdn.net/cuker919/article/details/5957209 在service类前加上@Transactional,声明这个se ...
- Spring事务的传播行为 @Transactional(转)
Spring事务的传播行为 在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例 ...
随机推荐
- Oracle 外键级联更新
Oracle数据库中,外键约束只允许级联删除,不允许级联更新,因此,如果想要实现主表数据更新后,子表外键自动更新,只能取消外键关系,通过前端程序来维护实现完整引用,一个代替的解决方案是使用延迟约束和触 ...
- mybatis中大于等于小于等于的写法
标准写法如下: 第一种写法(1): 原符号 < <= > >= & ' "替换符号 < <= > >= & &apos ...
- ORC 文件存储格式
1.orc列式存储概念 a)列式存储:orc并不是纯粹的列式存储,也是先基于行对数据表进行分组(行组),然后对行组进行列式存储. b)查询数据的时候不需要扫描全部数据(磁盘IO),只需查询指定列即可. ...
- Python 之 __new__() 方法与实例化
原文链接:https://www.cnblogs.com/ifantastic/p/3175735.html __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解, ...
- MySQL按周统计 WEEK 实例
MySQL按周统计每周数据总和,用到了WEEK,subdate,date_format,date_sub,date_add函数. WEEK() 查看给定日期周数,语法:WEEK(date, mode) ...
- 刘志梅201771010115.《面向对象程序设计(java)》第十四周学习总结
实验十四 Swing图形界面组件 实验时间 20178-11-29 1.实验目的与要求 (1)设计模式:反复使用.经过分类编目的.代码设计经验的总结. 在Alexander的模式分类和软件模式的分类 ...
- C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点
#region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...
- Javascript面向对象编程(一)
什么是面向对象? 它是一种新的编程模式.以往的面向过程是将功能都写在一起,逐行实现.面向对象与其不同,它只关心对象提供的功能,而不在乎细节. 面向对象的特点? 抽象:抓住问题的核心,贯穿始终: 封装: ...
- 常用LINUX命令汇总
一.基本命令bash Bash(GNU Bourne-Again Shell)是许多Linux平台的内定Shellpwd 查看当前所在目录ls 查看目录内所有文件cd 进入目录cd .. 返回上一层p ...
- hive 学习
多表查询 SELECT a.user_uid_type, a.user_uid, c.user_id, c.user_type FROM mytable1 a JOIN mytable2 b ON(a ...