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

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

    Service层

    Service层将方法的事务传播属性设置为SUPPORTS

    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  * 测试Supports
    13  * @author yangchaolin
    14  *
    15  */
    16 @Component("supportTest")
    17 public class SupportedTest {
    18     @Resource(name="service1")
    19     EMPService1 service1;
    20     @Resource(name="service2")
    21     EMPService2 service2;
    22     
    23     /**
    24      * 外层方法没有事务,但是抛出异常  
    25      * @param emp1
    26      * @param emp2
    27      */
    28     public void testSupportsWithoutTransaction1(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 testSupportsWithoutTransaction2(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 testSupportsWithTransaction1(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 testSupportsWithTransaction2(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 testSupportsWithTransaction3(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 }

    测试代码

     1 package TestCase;
     2 
     3 import org.junit.Test;
     4 
     5 import Entity.EMP;
     6 import LayerT.SupportedTest;
     7 
     8 public class supportsTestCase extends baseTest{
     9     @Test
    10     public void test1() {
    11         SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
    12         EMP emp1=new EMP("张三",18);
    13         EMP emp2=new EMP("李四",28);
    14         T1.testSupportsWithoutTransaction1(emp1, emp2);
    15       }    
    16     @Test
    17     public void test2() {
    18         SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
    19         EMP emp1=new EMP("张三",18);
    20         EMP emp2=new EMP("李四",28);
    21         T1.testSupportsWithoutTransaction2(emp1, emp2);
    22       }    
    23     @Test
    24     public void test3() {
    25         SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
    26         EMP emp1=new EMP("张三",18);
    27         EMP emp2=new EMP("李四",28);
    28         T1.testSupportsWithTransaction1(emp1, emp2);
    29       }    
    30     @Test
    31     public void test4() {
    32         SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
    33         EMP emp1=new EMP("张三",18);
    34         EMP emp2=new EMP("李四",28);
    35         T1.testSupportsWithTransaction2(emp1, emp2);
    36       }    
    37     @Test
    38     public void test5() {
    39         SupportedTest T1=ac.getBean("supportTest",SupportedTest.class);
    40         EMP emp1=new EMP("张三",18);
    41         EMP emp2=new EMP("李四",28);
    42         T1.testSupportsWithTransaction3(emp1, emp2);
    43       }    
    44 }

    测试结果

    (1)外层方法没有事务

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

    结论:当内层方法事务传播属性设置为SUPPORTS时,在外层方法没有声明事务的情况下,按照非事务方式执行,所以test2方法执行后依然能插入李四。

    (2)外层方法有事务

    test3 张三未插入,李四未插入
    test4 张三未插入,李四未插入
    test5 张三未插入,李四未插入

    结论:当外层方法支持事务传播时,内层事务加入到外层事务,绑定到一起,只要其中一个发生异常,不管内层方法执行时是否捕获异常,整个事务都需要回滚,这点类似Required。

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

  • 相关阅读:
    Selenium WebDriver
    Xpath常用语法
    firefox插件
    Echarts图中datazoom的相关配置
    js获取当前年月日
    echarts饼图角度起始角度位置和生长方向
    jS获取table表格任意单元格值
    jquery中 cache: true和false的区别
    Ajax请求中的async:false/true的作用 、cache: true和false的区别
    Cocos Creator set get
  • 原文地址:https://www.cnblogs.com/youngchaolin/p/10629933.html
Copyright © 2011-2022 走看看