1. 程式人生 > >Spring中 PROPAGATION_REQUIRED 解釋

Spring中 PROPAGATION_REQUIRED 解釋

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd"
       	   default-autowire="byName">
       	   
    <import resource="classpath*:spring/spring-config-dao.xml" />
    <context:component-scan base-package="com.letv.*.manager" />
    <aop:aspectj-autoproxy proxy-target-class="true" />

	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="query*" propagation="REQUIRED" read-only="true"/>    
			<tx:method name="save*" propagation="REQUIRED"/> 
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="batchSave*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="cancelOrder" propagation="REQUIRED"/>
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />     
		</tx:attributes>
	</tx:advice>
	  
	<aop:config>
		<aop:pointcut id="managers" expression="execution(* com.thb.*.manager..*.*(..)) "/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="managers"/>
	</aop:config>
</beans>

上面配置檔案意思是呼叫 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);
    }