接下来测试事务传播属性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. docker systemctl无法使用

    Dockerfile for systemd base image FROM centos:7 ENV container docker RUN (cd /lib/systemd/system/sys ...

  2. InnoDB 存储引擎的特点及优化方法

    InnoDB:   InnoDB 引擎是MySQL数据库的另一个重要的存储引擎.被包含在所有的二进制安装包里面.和其它的存储引擎相比.InnoDB引擎的优点是支持兼容ACID的事物.以及参数完整性(有 ...

  3. pip改源

    临时 python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple django==1.10 # ==后边指定版本号,也可以不指定 ...

  4. MariaDB——(一)CentOS 6.5 下 MariaDB 10.0.15 YUM 安装

    1.配置yum源: 在MariaDB官网提供了yum源在线生成器,选择合适的系统和版本后,会生成所需的repo文件内容: 在/etc/yum.repos.d/目录下新建一个MariaDB.repo文件 ...

  5. 使用R的注意事项

    换源,将源切换到中国 具体方法是: options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/&qu ...

  6. Druid密码加密

    pom里引用: <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring ...

  7. python_hashlib模块

    1 网站登陆 import random import string import pickle # data1 = string.ascii_letters # print("data1数 ...

  8. 3、谈谈 Java NIO

    在 JDK1.4 之后,为了提高 Java IO 的效率,Java 提供了一套 New IO (NIO),之所以称之为 New,原因在于它相对于之前的 IO 类库是新增的.此外,旧的 IO 类库提供的 ...

  9. airflow整体架构

    run命令运行过程 读取dag文件生成task依赖关系,然后生成封装airflow run的command命令,通过celery发送到executor端,重新执行该airflow run命令. sch ...

  10. 使用腾讯地图和js,html实现地理位置的获取

    转自http://blog.csdn.net/eele_one/article/details/64905218<!DOCTYPE html> <html> <head& ...