Spring中 PROPAGATION_REQUIRED 解释
2017-01-01 16:04
225 查看
事务传播行为种类
Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,
它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播:
事务传播行为类型
实例为
上面配置文件意思是调用 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);
}
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);
}
相关文章推荐
- codeforce#362B. Barnicle(java高精度类)
- JAVA_weB中的一些配置
- Spring 通过Autowired 和 Context.getBean()方式拿到的不是同一个Bean
- (五)WebService之CXF和Spring整合
- java -面向对象编程-类与对象
- MyEclipse安装插件的几种方法
- Java 匿名对象的详解
- Java中分割字符串
- JAVA中获取当前系统时间及格式转换
- JDK、SDK、NDK、ADT 、ANT名词解释
- 疯狂Java笔记
- java接口学习
- Java 基于JDK中的InvocationHandler实现动态代理
- Spring + Spring MVC + mybatis 下的 junit4 注入单元测试
- 小博老师解析Java核心技术 ——JSwing键盘监听事件
- 步步认识java-Collection的常用方法
- 博为峰Java技术文章 ——JavaSE Swing 单选按钮Ⅰ
- Spring+MyBatis整合开发时 Injection of autowired dependencies failed错误的解决
- eclipse之断点调试小技巧
- 深入JDK源码之ThreadLocal类