接下来测试事务传播属性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的更多相关文章

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

    在准备好前期的项目搭建后,接下来就一个个的测试,首先测试事务传播的Required Service层两个实现类 Service层两个实现类,只是更换了方法事务传播的属性,其他都一样,后续测试也只修改传 ...

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

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

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

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

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

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

  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事务的传播行为http://blog.csdn.net/cuker919/article/details/5957209 在service类前加上@Transactional,声明这个se ...

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

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

随机推荐

  1. java 乱码问题集

    场景1:刚复制来的java类乱码,反复修改无果 解:将java类用NotePat++打开,可正常显示,复制过来即可.

  2. 猜测的rpc负载均衡原理,基于dubbo的架构

    集群层(Cluster):封装多个提供者的路由及负载均衡,并桥接注册中心,以Invoker为中心,扩展接口为Cluster.Directory.Router和LoadBalance.将多个服务提供方组 ...

  3. 流程帮App风险评估

    一. 存在风险 此处罗列出了我们开发小组可能遇到8种的风险. 编号 风险名称 内容 发生概率 损失(人周) 危险度(周) 1 计划编制风险 对所要使用技术不熟悉,可能导致无法交付: 每个模块的实现一定 ...

  4. g2opy 记录

    20190429 10:31 之前安装了g2opy,g2o也一直没好好学,跑通了slam14讲第13章的单目稠密重建.想要改写成 python版,开始仔细研究g2opy的用法.

  5. Linux基础之常用命令整理(二)

    Linux系统启动流程 bios(找到启动介质) --> mbr(找到boot loader  512B 446引导信息 64分区信息 2 标志位 ) -->grub(选择操作系统或者内核 ...

  6. Why Lua?

    Why Lua? 最近在很多的地方都遇到了lua这个东西,于是想一探究竟,为什么这么多的游戏前端都用了Lua脚本呢? 于是乎简单的看了一下Lua,目前总结出来了几点~ 还是先放上资源: GitHub上 ...

  7. C博客01——分支,顺序结构

    C博客01--分支,顺序结构 1. 本章学习总结 1.1 思维导图 请以思维导图总结本周的学习内容. 1.2 本章学习体会及代码量体会 1.2.1 学习体会 对于C语言课程的理解,我有点吃力,不是说老 ...

  8. c# 访问共享文件

    #region 连接共享文件夹 /// <summary> /// 连接共享文件夹 /// </summary> public bool ConnectToSharedFold ...

  9. Robot Framework 自动化框架大纲

    Python + Robot Framework 环境搭建 Android SDK + Appium 环境搭建 RobotFramework - AppiumLibrary 之元素定位 RobotFr ...

  10. Cache-control使用Cache-control:private学习笔记【转载】

    网页缓存由 HTTP消息头中的Cache-control控制,常见取值有private.no-cache.max-age.must- revalidate等,默认为private 其作用根据不同的重新 ...