zoukankan      html  css  js  c++  java
  • spring交易声明的几个传播特性

     近期遇到了一个spring事务导致的问题,所以写了几个小程序了解了一下事务的传播特性,以下分别举样例分别看看事务的传播特性。

     

            事务的几种传播特性

    1. PROPAGATION_REQUIRED: 假设存在一个事务,则支持当前事务。

    假设没有事务则开启

    Java代码  收藏代码
    1. /** 
    2.  * TransactionTestService     test1和test2配有事务(PROPAGATION_REQUIRED) */  
    3. public interface TransactionTestService {  
    4.     //事务属性 PROPAGATION_REQUIRED  
    5.     public void test1() throws Exception{  
    6.         avInfoTaskTunnel.insertAvInfoTask();  
    7.         test2();  
    8.     }  
    9.     //事务属性 PROPAGATION_REQUIRED  
    10.     public void test2() throws Exception{  
    11.         avRequestTunnel.insertAvRequest();  
    12.         throw new Exception();  
    13.     }  
    14. }  
    15.    
    16. /** 
    17.  * main 
    18.  */  
    19. public class TransactionTestMain {  
    20.    
    21.     public static void main(String[] args) throws Exception{  
    22.         TransactionTestService transactionTestService = (TransactionTestService)context.getBean("transactionTestService");  
    23.         try {  
    24.             transactionTestService.test1();  
    25.         } catch (Exception e) {  
    26.         }  
    27.     }  
    28. }  

            上述代码中test1()和test2()都配有PROPAGATION_REQUIRED事务属性,test1()内部调用test2(),这样test1()和test2()方法将都处于同一事务之中,当在test2()中抛出异常。会导致test1()和test2()方法中的事务都回滚。

           可是,假设test1()方法对调用test2()时捕获异常,结果会是如何的呢? test1应该能正常写入没问题,那么test2呢?

    Java代码  收藏代码
    1. //test1()中捕获test2()抛出的异常  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.         try {  
    5.             test2();  
    6.         }catch (Exception e) {  
    7.         }  
    8.    
    9.     }  

           最后的结果test2()也将会正常的写入。

    事实上,上面情况中假设仅仅是test1()配有PROPAGATION_REQUIRED事务属性。test2()不配置不论什么事务属性。发生的结果也是一致的。上面的情形相当于:

    Java代码  收藏代码
    1. public static void main(String[] args) throws Exception{  
    2.         Connection con = null;  
    3.         try {  
    4.             con=getConnection();  
    5.             con.setAutoCommit(false);  
    6.             transactionTestService.test1(); //test1()和test2()处于同一事务之中  
    7.             con.commit();  
    8.         } catch (Exception e) {  
    9.             con.rollback();  
    10.         } finally {  
    11.             closeCon();  
    12.         }  
    13.     }  

             上述test1()和test2()是同一个类的两个方法,那么要是它们处于不同类呢?

    Java代码  收藏代码
    1. //main函数不变,test1()中调用test2()地方换成调用还有一个类中配有PROPAGATION_REQUIRED事务属性的方法  
    2.     //不正确otherService.test2()捕获异常的情形就不是必需说了。必然都回滚。  
    3.     public void test1() throws Exception{  
    4.         avInfoTaskTunnel.insertAvInfoTask();  
    5.         try {  
    6.             otherService.test2();  //PROPAGATION_REQUIRED事务属性  
    7.         } catch (Exception e) {  
    8.         }  
    9.     }  
    10.    
    11.     //otherService.test2()  
    12.     public void test2() throws Exception{  
    13.         avRequestTunnel.insertAvRequest();  
    14.         throw new Exception();  
    15.     }  

             上述相同捕获了异常,可是结果会如何呢? 结果有点出乎意料。与之前test1(),test2()处于同一类的情形不同,这个时候。两个方法都将回滚。而且在调用test1()的地方会抛出以下异常。

    这是因为子事务在回滚的时候已经将主事务标记成了rollback-only,这样导致主事务在提交的时候就会抛出以下这个异常。

    另外:假设otherService.test2()没有配置不论什么事务属性,那么test2()抛出异常的时候,将导致test1()和test2()都回滚。

    Java代码  收藏代码
    1. org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only  

            上述情况网上查询到一种解决方案是在transactionManager中将globalRollbackOnParticipationFailure 设置为false(默认是true)。可是这样又带来还有一个问题,子事务也给一并提交了(这个时候子事务产生异常,不想提交),详细的解决方案还没找到。可是我认为不应该将这两个配置在同一事务中。

    Xml代码  收藏代码
    1. <bean id="transactionManager"      
    2.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    3.         <property name="dataSource">  
    4.             <ref local="dataSource" />  
    5.         </property>  
    6.         <property name="globalRollbackOnParticipationFailure" value="false" />   
    7.     </bean>  

     

    2. PROPAGATION_SUPPORTS: 假设存在一个事务,支持当前事务。假设没有事务,则非事务的运行
    Java代码  收藏代码
    1. //TransactionTestService     test1()配有事务(PROPAGATION_SUPPORTS)  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.         throw new Exception();  
    5.     }  
    6.    
    7.    
    8. /** 
    9.  * main 
    10.  */  
    11.     public class TransactionTestMain {  
    12.    
    13.         public static void main(String[] args) throws Exception{  
    14.             TransactionTestService transactionTestService = (TransactionTestService)context.getBean("transactionTestService");  
    15.             try {  
    16.                 transactionTestService.test1();  
    17.             } catch (Exception e) {  
    18.             }  
    19.        }  
    20.     }  

             TransactionTestService的test1()配有PROPAGATION_SUPPORTS事务属性,我们知道这样的情形下假设配的是PROPAGATION_REQUIRED事务属性。那么test1()将新建事务执行。可是PROPAGATION_SUPPORTS属性不会新建,这样的情形下,test1()方法将正常提交。那假设是外层事务配有事务呢?例如以下所看到的,此种情形test2()将处于事务之中了。

    Java代码  收藏代码
    1. //PROPAGATION_REQUIRED事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.         test2();  
    5.     }  
    6.     //PROPAGATION_SUPPORTS事务属性  
    7.     public void test2() throws Exception{  
    8.         avRequestTunnel.insertAvRequest();  
    9.         throw new Exception();  
    10.     }  

     

      3. PROPAGATION_MANDATORY: 假设已经存在一个事务,支持当前事务。假设没有一个活动的事务。则抛出异常。

     

    Java代码  收藏代码
    1. //情形1:PROPAGATION_REQUIRED事务属性  情形2:不配不论什么事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.         otherService.test2();  
    5.     }  
    6.     //otherService的test2()配置PROPAGATION_MANDATORY事务属性  
    7.     public void test2() throws Exception {  
    8.         avRequestTunnel.insertAvRequest();  
    9.         throw new Exception();  
    10.     }  
    11.    
    12. //test1()处于情形2时抛出异常,test2()不可以提交,可是test1()却是可以成功提交的  
    13. org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'  

             上述情况,当test1()处于情形1时,会是的test1()和test2()都处于事务其中;test1()处于情形2时,就会抛异常,可是这个时候test2()不可以提交,可是test1()却是可以成功提交的。此外,另一点,注意上述test2()是处于另一个类中的,假设是处于同一个类。那么PROPAGATION_MANDATORY不会由于外层是否有事务而抛异常。

    4. PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。假设一个事务已经存在,则将这个存在的事务挂起。

    Java代码  收藏代码
    1. //情形1:PROPAGATION_REQUIRED事务属性  情形2:不配不论什么事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.         otherService.test2();  
    5.     }  
    6.     //otherService的test2()配置PROPAGATION_REQUIRES_NEW事务属性  
    7.     public void test2() throws Exception {  
    8.         avRequestTunnel.insertAvRequest();  
    9.         throw new Exception();  
    10.     }  

     上述情况,test1()处于情形2时test2()新建事务。这点没有问题。那假设test1()处于情形1呢?也就是说test1()已经有事务了。结果是test2()处于新的事务中,怎么确定是处于新的事务中呢?看以下代码:

    Java代码  收藏代码
    1. //test1配置PROPAGATION_REQUIRED事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.             otherService.test2();  //PROPAGATION_REQUIRES_NEW事务属性  
    5.     }  
    6.    
    7.     //otherService.test2()  
    8.     public void test2() throws Exception{  
    9.         avRequestTunnel.insertAvRequest();  
    10.         throw new Exception();  
    11.     }  

             假设是在同一事务中。那么情形将同于讲述PROPAGATION_REQUIRED属性时的情形。test1()和test2()都将回滚,而且抛出异常事务被打上rollback-only标记的异常。可是这里,结果就是test2()回滚,test1正常提交,所以otherService.test2()处于新的事务中。注意:上述情况test2()也是还有一个类的方法,假设属于同一类,也就是test1()和test2()处于同一个类。test1()中调用test2(),那么配置的PROPAGATION_REQUIRES_NEW将无效(跟test2()什么事务属性都没配置一样)。

    可是假设是main函数中直接调用test2(),那么还是会起一个新的事务。

            第二种证明test1()和test2()处于不同事务的方式是,在test2()不抛出异常,然后再test1()调用了test2()之后,抛出异常,最后结果是()回滚。test2()正常提交。

     

    5. PROPAGATION_NOT_SUPPORTED: 总是非事务地运行,并挂起不论什么存在的事务。
    Java代码  收藏代码
    1. //test1配置PROPAGATION_REQUIRED事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.             otherService.test2();  //PROPAGATION_NOT_SUPPORTED事务属性  
    5.     }  
    6.    
    7.     //otherService.test2()  
    8.     public void test2() throws Exception{  
    9.         avRequestTunnel.insertAvRequest();  
    10.         throw new Exception();  
    11.     }  

             假设otherService.test2() 没有配置事务属性,那么test2()抛出异常时,test1()和test2()都回滚。可是如今test2()配置了PROPAGATION_NOT_SUPPORTED事务属性,那么test2()将以非事务执行,而test1()继续执行在事务中。也就是说,此时,test1()回滚,test2()正常提交。

    注意:假设test1()和test2()在同一类中。那么test2()的PROPAGATION_NOT_SUPPORTED失效。

     

    6. PROPAGATION_NEVER: 总是非事务地运行。假设存在一个活动事务,则抛出异常
    Java代码  收藏代码
    1. //test1配置PROPAGATION_REQUIRED事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.             otherService.test2();  //PROPAGATION_NEVER事务属性  
    5.     }  
    6.    
    7.     //otherService.test2()  
    8.     public void test2() throws Exception{  
    9.         avRequestTunnel.insertAvRequest();  
    10.         throw new Exception();  
    11.     }  
    12. //test1()配置PROPAGATION_REQUIRED事务属性, otherService.test2()配置PROPAGATION_NEVER事务属性,将抛以下异常:  
    13. org.springframework.transaction.IllegalTransactionStateException: Existing transaction found for transaction marked with propagation 'never'  

     当然上述情况。假设是test1()和test2()在同一类中。那么PROPAGATION_NEVER也将失效。

     

    7. PROPAGATION_NESTED:假设一个活动的事务存在,则执行在一个嵌套的事务中. 假设没有活动事务, 则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行

            最easy弄混淆的事实上是 PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED。

    PROPAGATION_REQUIRES_NEW 启动一个新的, 不依赖于环境的 "内部" 事务. 这个事务将被全然 commited 或 rolled back 而不依赖于外部事务, 它拥有自己的隔离范围, 自己的锁, 等等. 当内部事务開始运行时, 外部事务将被挂起, 内务事务结束时, 外部事务将继续运行。还有一方面, PROPAGATION_NESTED 開始一个 "嵌套的" 事务, 它是已经存在事务的一个真正的子事务. 嵌套事务開始运行时, 它将取得一个 savepoint. 假设这个嵌套事务失败, 我们将回滚到此 savepoint.。嵌套事务是外部事务的一部分, 仅仅有外部事务结束后它才会被提交。

    由此可见, PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED 的最大差别在于, PROPAGATION_REQUIRES_NEW 全然是一个新的事务, 而 PROPAGATION_NESTED 则是外部事务的子事务, 假设外部事务 commit, 潜套事务也会被 commit, 这个规则相同适用于 roll back.

    Java代码  收藏代码
    1. //test1配置PROPAGATION_REQUIRED事务属性  
    2.     public void test1() throws Exception{  
    3.         avInfoTaskTunnel.insertAvInfoTask();  
    4.             otherService.test2();  //PROPAGATION_NESTED事务属性  
    5.     }  
    6.    
    7.     //otherService.test2()  
    8.     public void test2() throws Exception{  
    9.         avRequestTunnel.insertAvRequest();  
    10.     }  

             上述情况。假设otherService.test2()配置PROPAGATION_REQUIRES_NEW事务属性。这样test1()回滚,可是test2()正常提交,由于这是两个事务。可是假设otherService.test2()配置PROPAGATION_NESTED事务属性。然后test1()和test2()将回滚。

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    JS实现前台表格排序功能
    openoffice安装手记
    OpenOffice 实现OFFICE在线预览
    毫秒事件转换小方法
    axis2 jar包详解及缺少jar包异常分析
    Android EditText控件完美实现只读(ReadOnly/NonEditable)
    android:获取联系人信息(姓名和电话)
    dex2jar.bat反编译apk的classes.dex文件错误:
    服务器压力测试系列二:服务器监控工具tsar安装
    memcache list all keys
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4668176.html
Copyright © 2011-2022 走看看