zoukankan      html  css  js  c++  java
  • 云笔记项目-Spring事务学习-传播MANDATORY

    接下来测试事务传播属性MANDATORY

    Service层

    所有Service层实现类都设置事务传播属性为MANDATORY。

    LayerT层代码

     1 package LayerT;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.springframework.stereotype.Component;
     6 import org.springframework.transaction.annotation.Transactional;
     7 
     8 import Entity.EMP;
     9 import Service.EMPService1;
    10 import Service.EMPService2;
    11 
    12 /**
    13  * 测试Mandatory
    14  * @author yangchaolin
    15  *
    16  */
    17 @Component("mandatoryTest")
    18 public class MandatoryTest {
    19     @Resource(name="service1")
    20     EMPService1 service1;
    21     @Resource(name="service2")
    22     EMPService2 service2;
    23     /**
    24      * 外层方法没有事务,但是抛出异常
    25      * @param emp1
    26      * @param emp2
    27      */
    28     public void testMandatoryWithoutTransaction1(EMP emp1,EMP emp2) {
    29         service1.addEmp1(emp1);
    30         service2.addEmp2(emp2);
    31         throw new RuntimeException();
    32     }
    33     /**
    34      * 外层方法没有事务,内层方法抛出异常
    35      * @param emp1
    36      * @param emp2
    37      */
    38     public void testMandatoryWithoutTransaction2(EMP emp1,EMP emp2) {
    39         service1.addEmp1(emp1);
    40         service2.addEmp2WithException(emp2);
    41     }
    42     /**
    43      * 外层方法有事务,并且抛出异常
    44      * @param emp1
    45      * @param emp2
    46      */
    47     @Transactional
    48     public void testMandatoryWithTransaction1(EMP emp1,EMP emp2) {
    49         service1.addEmp1(emp1);
    50         service2.addEmp2(emp2);
    51         throw new RuntimeException();
    52     }
    53     /**
    54      * 外层方法有事务,内层方法抛出异常
    55      * @param emp1
    56      * @param emp2
    57      */
    58     @Transactional
    59     public void testMandatoryWithTransaction2(EMP emp1,EMP emp2) {
    60         service1.addEmp1(emp1);
    61         service2.addEmp2WithException(emp2);
    62     }
    63     /**
    64      * 外层方法有事务,内层方法抛出异常被捕获
    65      * @param emp1
    66      * @param emp2
    67      */
    68     @Transactional
    69     public void testMandatoryWithTransaction3(EMP emp1,EMP emp2) {
    70         service1.addEmp1(emp1);
    71         try {
    72         service2.addEmp2WithException(emp2);
    73         }catch(Exception e) {
    74             System.out.println("回滚");
    75         }
    76     }
    77     /**
    78      * 外层方法有事务,没有异常发生
    79      * @param emp1
    80      * @param emp2
    81      */
    82     @Transactional
    83     public void testMandatoryWithTransaction4(EMP emp1,EMP emp2) {
    84         service1.addEmp1(emp1);
    85         service2.addEmp2(emp2);
    86     }
    87 }

    测试代码

     1 package TestCase;
     2 
     3 import org.junit.Test;
     4 
     5 import Entity.EMP;
     6 import LayerT.MandatoryTest;
     7 
     8 public class mandatoryTestCase extends baseTest{
     9     @Test
    10     public void test1() {
    11         MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
    12         EMP emp1=new EMP("张三",18);
    13         EMP emp2=new EMP("李四",28);
    14         T1.testMandatoryWithoutTransaction1(emp1, emp2);
    15       }    
    16     @Test
    17     public void test2() {
    18         MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
    19         EMP emp1=new EMP("张三",18);
    20         EMP emp2=new EMP("李四",28);
    21         T1.testMandatoryWithoutTransaction2(emp1, emp2);
    22       }    
    23     @Test
    24     public void test3() {
    25         MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
    26         EMP emp1=new EMP("张三",18);
    27         EMP emp2=new EMP("李四",28);
    28         T1.testMandatoryWithTransaction1(emp1, emp2);
    29       }    
    30     @Test
    31     public void test4() {
    32         MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
    33         EMP emp1=new EMP("张三",18);
    34         EMP emp2=new EMP("李四",28);
    35         T1.testMandatoryWithTransaction2(emp1, emp2);
    36       }    
    37     @Test
    38     public void test5() {
    39         MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
    40         EMP emp1=new EMP("张三",18);
    41         EMP emp2=new EMP("李四",28);
    42         T1.testMandatoryWithTransaction3(emp1, emp2);
    43       }    
    44     @Test
    45     public void test6() {
    46         MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class);
    47         EMP emp1=new EMP("张三",18);
    48         EMP emp2=new EMP("李四",28);
    49         T1.testMandatoryWithTransaction4(emp1, emp2);
    50       }    
    51 }

    测试结果

    (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

  • 相关阅读:
    1026. 程序运行时间(15)
    C语言字符串/数组去重
    1025. 反转链表 (25)
    1024. 科学计数法 (20)
    1023. 组个最小数 (20)
    1022. D进制的A+B (20)
    1021. 个位数统计 (15)
    1020. 月饼 (25)
    前端001/正则表达式
    SSM001/构建maven多模块项目
  • 原文地址:https://www.cnblogs.com/youngchaolin/p/10629795.html
Copyright © 2011-2022 走看看