zoukankan      html  css  js  c++  java
  • Spring中 PROPAGATION_REQUIRED 解释

    转自:https://blog.csdn.net/bigtree_3721/article/details/53966617

    事务传播行为种类

    Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,

    它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播:

    事务传播行为类型

    事务传播行为类型

    说明

    PROPAGATION_REQUIRED

    如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

    PROPAGATION_SUPPORTS

    支持当前事务,如果当前没有事务,就以非事务方式执行。

    PROPAGATION_MANDATORY

    使用当前的事务,如果当前没有事务,就抛出异常。

    PROPAGATION_REQUIRES_NEW

    新建事务,如果当前存在事务,把当前事务挂起。

    PROPAGATION_NOT_SUPPORTED

    以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

    PROPAGATION_NEVER

    以非事务方式执行,如果当前存在事务,则抛出异常。

    PROPAGATION_NESTED

    如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类 似的操作。


    实例为
    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:p="http://www.springframework.org/schema/p"  
    5.        xmlns:context="http://www.springframework.org/schema/context"  
    6.        xmlns:aop="http://www.springframework.org/schema/aop"  
    7.        xmlns:tx="http://www.springframework.org/schema/tx"  
    8.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
    9.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    10.             http://www.springframework.org/schema/aop   
    11.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
    12.             http://www.springframework.org/schema/tx   
    13.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    14.             http://www.springframework.org/schema/context   
    15.             http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
    16.            default-autowire="byName">  
    17.              
    18.     <import resource="classpath*:spring/spring-config-dao.xml" />  
    19.     <context:component-scan base-package="com.letv.*.manager" />  
    20.     <aop:aspectj-autoproxy proxy-target-class="true" />  
    21.   
    22.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    23.         <property name="dataSource" ref="dataSource"/>  
    24.     </bean>  
    25.       
    26.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    27.         <tx:attributes>  
    28.             <tx:method name="query*" propagation="REQUIRED" read-only="true"/>      
    29.             <tx:method name="save*" propagation="REQUIRED"/>   
    30.             <tx:method name="insert*" propagation="REQUIRED"/>  
    31.             <tx:method name="batchSave*" propagation="REQUIRED"/>  
    32.             <tx:method name="update*" propagation="REQUIRED"/>  
    33.             <tx:method name="cancelOrder" propagation="REQUIRED"/>  
    34.             <tx:method name="*" propagation="SUPPORTS" read-only="true" />       
    35.         </tx:attributes>  
    36.     </tx:advice>  
    37.         
    38.     <aop:config>  
    39.         <aop:pointcut id="managers" expression="execution(* com.thb.*.manager..*.*(..)) "/>  
    40.         <aop:advisor advice-ref="txAdvice" pointcut-ref="managers"/>  
    41.     </aop:config>  
    42. </beans>  
    [html] view plain copy
     
    1.   

    上面配置文件意思是调用 com.thb.下面的任何的任何manager类里的以query,save, update等开头的方法和 cancelorder方法时,在方法层面上事务级别是required,而调用这些manager的别的方法则是support级别。

    比如 在下面的 com.letv.ofc.manager.impl.updateStrategyResult() 方法中,又调用了几个别的manager的方法,则这几个别的manager的方法都是required,它们在updateStrategyResult()中会共用一个transaction。结果是这几个方法的SQL 执行只要有一个失败,都会导致其它的方法回滚 (rollback)

     public void updateStrategyResult() {
          ....      
            saveOrderInfo(orderInfo, remark);
            insertOFCSourcingOrderTask(orderQuery);
           updateTaskOrder(task, grabObject);
            insertWMSDistributionOrderTask(task);
            insertOperateLog(orderInfo.getOrderCode(), remark);
            lockSkuStock(grabObject);
        }

  • 相关阅读:
    程序员的 59 条搞笑但却真实无比的编程语录
    Github最流行的10,000个Java项目使用的类库
    10大怪异的编程语言
    如何写一篇好的技术博客
    四件在我步入职业软件开发生涯那天起就该知道的事情
    程序员最艰巨的十大任务
    10 个理由让你继续干 IT
    被诅咒的程序员的七宗罪
    顶级程序员的 10 条最佳实践
    谷歌如何管理世界上最聪明的工程师?
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/8656676.html
Copyright © 2011-2022 走看看