Spring中 PROPAGATION_REQUIRED 解释 事物是在一个方法里调用其他的方法,一起成功或者一起失败,是方法之间的关系,而不是某一个方法内部的问题

事务传播行为种类

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);
    }

猜你喜欢

转载自www.cnblogs.com/shenzhichipingguo/p/9186093.html