接下来测试事务传播属性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. oracle 远程连接

    oracle中如何修改用户名和密码   1.以Windows操作系统为例,打开命令提示符,输入命令sqlplus /nolog ,进入oracle控制台,并输入 conn /as sysdba;以DB ...

  2. 安装rpm

    剩余 gcc-c++-3.4.6-3.1.x86_64.rpm elfutils-libelf-devel-0.97-5.x86_64.rpm glibc-2.3.4-2.41.x86_64.rpm ...

  3. 嵌入式全志V3s荔枝派板卡移植 aircrack-ng

    我使用的是aircrack-ng-1.1.tar.gz这个版本的包. 编辑common.mak,在27行COMMON_FLAGS = 下加入嵌入式开发的环境 -I 和 -L : COMMON_CFLA ...

  4. 如何重置Gitlab root用户密码

    一.切换到root用户 sudo su 二.进入gitlab控制台 gitlab-rails console production 三.查找用户对象 user = User.).first 四.重置密 ...

  5. ajax(读取json数据)

    ajax知识点: 语法:$.ajax(路由,请求方式,返回的数据类型,数据参数,回调函数) url: "路由", type:"",默认get请求(get或pos ...

  6. idea引用本地jar包的方法及报错解决

    1 首先将本地jar包打入mvn本地仓库 cmd mvn install:install-file -Dfile=E://xx.jar 本地jar包绝对路径 -DgroupId=com.ccp -Da ...

  7. mybatis缓存的设计

    继续用提问的方式来看Mybatis的缓存设计. 1.Mybatis如何开启缓存 Mybatis对查询结果进行缓存,所以缓存的对象为具体的Statement 通过在Statement上是否使用缓存来启用 ...

  8. 获取cpu和内存使用情况

    public class SystemInfo { [DllImport("kernel32")] public static extern void GetSystemDirec ...

  9. 机器学习笔记之三-yolov3+win7+vs2017+gpu+opencv编译

    1.环境安装 1.1 vs2017+cuda9.1+cudnn7.0可以和tensorflow一起安装网上教程多,不多说.       唯一需要注意的是vs2017要安装好2015版本的工具集v140 ...

  10. Jmeter发送邮件功能SMTP Sampler

    介绍Jmeter的发送邮件功能,使用的Sampler是SMTP Sampler,详细说明每个配置项的功能 从上往下介绍需要用到的配置项: Server settings Server: 服务器地址 P ...