接下来测试事务传播属性设置为NOT_SUPPORTED

Service层

Service层主要设置如下,其中还插入了REQUIRED作为比较。

 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.NOT_SUPPORTED)
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.NOT_SUPPORTED)
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.Transactional; import Entity.EMP;
import Service.EMPService1;
import Service.EMPService2;
@Component("noteSupportedTest")
public class NotSupportedTest {
@Resource(name="service1")
EMPService1 service1;
@Resource(name="service2")
EMPService2 service2;
/**
* 外层方法没有事务,但是抛出异常
* @param emp1
* @param emp2
*/
public void testNotSupportWithoutTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2(emp2);
throw new RuntimeException();
}
/**
* 外层方法没有事务,内层方法抛出异常
* @param emp1
* @param emp2
*/
public void testNotSupportWithoutTransaction2(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);
service2.addEmp2WithException(emp2);
}
/**
* 外层方法有事务,并且抛出异常
* @param emp1
* @param emp2
*/
@Transactional
public void testNotSupportWithTransaction1(EMP emp1,EMP emp2) {
service1.addEmp1(emp1);//not_supported
service2.addEmp2(emp2);//required
throw new RuntimeException();
}
/**
* 外层方法有事务,并且内存方法抛出异常
* @param emp1
* @param emp2
* @param emp3
*/
@Transactional
public void testNotSupportWithTransaction2(EMP emp1,EMP emp2, EMP emp3) {
service1.addEmp1(emp1);//not_supported
service2.addEmp2(emp2);//required
service2.addEmp2WithException(emp3);//not_supported
}
}

测试代码

 package TestCase;

 import org.junit.Test;

 import Entity.EMP;
import LayerT.NotSupportedTest; public class notSupportedTestCase extends baseTest{
@Test
public void test1() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testNotSupportWithoutTransaction1(emp1, emp2);
}
@Test
public void test2() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testNotSupportWithoutTransaction2(emp1, emp2);
}
@Test
public void test3() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
T1.testNotSupportWithTransaction1(emp1, emp2);
}
@Test
public void test4() {
NotSupportedTest T1=ac.getBean("noteSupportedTest",NotSupportedTest.class);
EMP emp1=new EMP("张三",18);
EMP emp2=new EMP("李四",28);
EMP emp3=new EMP("王五",22);
T1.testNotSupportWithTransaction2(emp1, emp2, emp3);
}
}

测试结果

(1)外层方法没有事务

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

结论:当外层方法没有事务时,内层方法按照非事务方式执行。

(2)外层方法有事务

test3 张三插入,李四未插入
test4 张三插入,李四未插入,王五插入

结论:当外层方法事务传播属性设置为默认,只有声明REQUIRED的那个方法会受外层方法的影响,导致李四未插入,但是内层方法事务声明为NOT_SUPPORTED的方法,不论内层还是外层方法有异常,都不开启事务,出错也提交事务。

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

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

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

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

  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. java安装与配置

    参考:https://blog.csdn.net/gaokao2011/article/details/75211429 Win 1.JDK JDK 可以到官网下载http://www.oracle. ...

  2. HttpClient 上传图片

    Image image = Image.FromFile(@"F:\image2\DSC02028.JPG"); var data = ImageToByteArray(image ...

  3. office 32-bit components 2010 的卸载

    卸载方法:MsiExec.exe /X {90140000-0043-0000-1000-0000000FF1CE}

  4. linux 管道通信

    下面举linux下有名管道通信的代码. ----------------------------------------- fifo_read.c =========== #include<er ...

  5. C# 6.0:Exception Filter——带条件的异常处理

    C#6.0 对异常处理有两处改进,一个是在上一篇文章中我们讨论了的在catch和finally中使用await,另一个是exception filter.在catch和finally中使用await是 ...

  6. redis4.0.13主从、哨兵、集群3种模式的 Server端搭建、启动、验证

    本文使用的是redis-4.0.13.tar.gz版本. 两个centos7系统虚拟机:192.168.10.140.192.168.10.150 redis各版本下载地址:http://downlo ...

  7. OPC客户端开发问题总结

    环境准备 采用MatrikonOPC做模拟服务器,注册 OPCDAAuto.dll组件 引用 Interop.OPCAutomation.dll组件.开始开发. 1..new OPCServer()- ...

  8. oracle导出表的建表语句拼接SQL

    前段时间有个需求需要导出数据库的500张表结构,使用PLSQLDEV工具也可以导出建表语句,但是需要手动一个表一个表选,非常费劲.就写了个拼接sql. select 'select dbms_meta ...

  9. WPF-------依赖项属性

    http://www.cnblogs.com/Zhouyongh/archive/2009/09/10/1564099.html http://www.cnblogs.com/Zhouyongh/ar ...

  10. asp源码微信扫码授权登陆电脑版

    网站接入微信扫码登录并获取用户基本信息(完美绕过微信开放平台)电脑版网站实现微信扫码登录,注册会员还要设密码太麻烦,会员也记不住密码,采用微信扫码登录网站更方便,会员无需设密码,用他的微信做为系统登录 ...