Spring中 PROPAGATION_REQUIRED 解释

102 篇文章 2 订阅
12 篇文章 0 订阅

事务传播行为种类

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

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

事务传播行为类型

事务传播行为类型

说明

PROPAGATION_REQUIRED

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

PROPAGATION_SUPPORTS

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

PROPAGATION_MANDATORY

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

PROPAGATION_REQUIRES_NEW

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

PROPAGATION_NOT_SUPPORTED

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

PROPAGATION_NEVER

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

PROPAGATION_NESTED

如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与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);
    }


  • 7
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值