Spring 事务总结

rollbackFor 设为 Exception.class场景下

  1. 如果在函数内部catch住异常消费掉,没有再抛出的话,不会回滚

  2. 如果catch住 然后原封不动抛出,会回滚

  3. 如果catch住,然后改造成其他异常抛出,会回滚

  4. 如果是内层函数抛出,外层带事务的函数未抛出,也不会回滚

  5. 如果外层带事务函数catch住再抛出,会回滚

  6. 事务函数调用本类的public带有事务的函数,第二个函数不会带有事务,相当于一个普通函数,除非是调用其他类的事务函数

  7. 如果是@Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class) 调用的对象函数也是REQUIRED,则被调用函数成功抛出异常,即使外部函数catch住异常不抛,也会成功回滚,因为是同一个事务,aop在被调函数的增强中已经处理了回滚逻辑

两种配置方式

1. 注解配置 @EnableTransactionManagement,之后就可以@Transactional了

2.XML配置,resource下创建XML文件spring-tx.xml,之后@ImportResource(locations = {"classpath:spring-tx.xml"}),

XML配置还要引入jar:

		<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 拦截器方式配置事务 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="transactionPointcut" expression="within(com.grady.demotransaction..impl.*Impl) &amp;&amp; execution(* *(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
</beans>

aop:pointcut 的 expression 一定要配置正确, 之后就tx:method 中匹配的函数不需要@transactional了

3、 XML 配置注解型的事务

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- XML配置中开启事务,之后可用注解@Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

如果springboot版本大于2.1 ,properties 中要加上这个

spring:
main:
allow-bean-definition-overriding: true

经测试,同时XML配置注解版和拦截器版时,也是可以的,但建议只用其中之一

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- XML配置中开启事务,之后可用注解@Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 拦截器方式配置事务 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="test*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="transactionPointcut" expression="within(com.grady.demotransaction..impl.*Impl) &amp;&amp; execution(* *(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config> </beans>

最后建议还是使用注解的方式,更加明白直接,因为拦截器有可能由漏网之鱼,问题难以排查

最新文章

  1. Linux操作系统下三种配置环境变量的方法——转载
  2. KVM虚拟化技术简介
  3. 前台传到servlet的乱码问题要怎么处理
  4. JQuery 在$(window).load() 事件中 不运行 $(window).resize()
  5. Cortex-M3动态加载三(模块调用系统函数)
  6. CSS3 3D环境实现立体 魔方效果代码
  7. 使用Json.net对Json进行遍历
  8. argparse模块的应用
  9. oracle 定时 job
  10. lua 源码分析之线程对象lua_State
  11. 关注网页的更新状况,了解最新的handsup 消息.
  12. 【Mysql】mysql使用触发器创建hash索引
  13. Android An unexpected exception occurred while creating a change object. see the error log for more details
  14. 1052: 旋转单词(words)
  15. R的any和all
  16. jekins,报错 stderr: Could not create directory &#39;/usr/share/tomcat7/.ssh&#39;. Failed to add the host to the list of
  17. 【Cocos2d-X(1.x 2.x) 修复篇】iOS6 中 libcurl.a 无法通过armv7s编译以及iOS6中无法正常游戏横屏的解决方法
  18. AORUS GA-Z270X-Gaming 5開箱
  19. 为linux扩展swap分区
  20. 网站使用 rel=&quot;noopener&quot; 打开外部锚

热门文章

  1. WPF开发随笔收录-DrawingVisual绘制高性能曲线图
  2. 关于nginx 和 uwsgi
  3. Python快速下载商品数据,并连接数据库,保存数据
  4. Redis系列3:高可用之主从架构
  5. GitLab:Your account has been blocked.
  6. Neo4j应用
  7. NoSQL,关系型数据库,行列数据库对比、类比
  8. JS计算文本字符串字节长度和像素长度的方法
  9. PHP几个常见不常用的方法
  10. C#里如何简单的校验时间格式