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>

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

Spring_事务总结的更多相关文章

  1. Spring_事务管理

    转自:https://www.ibm.com/developerworks/cn/java/j-master-spring-transactional-use/index.html 事务管理是应用系统 ...

  2. Spring_事务-注解代码

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  3. Spring_事务

    事务管理: 用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当做一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性 原子性 一致性 隔离性 持久性 Spring两 ...

  4. Spring_事务(2)

  5. Spring_事务(1)

  6. Spring_事务准备

  7. spring 课程

    官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // ...

  8. Spring_使用XML文件的方式配置事务

    步骤: 正常配置jdbctemplate 正常配置bean 配置事物管理器 配置事物管理器 配置aop切入点,通过切入点把事物链接起来 思路: 接着上一个买书的例子吧,直接拷到新包下,把注解都干掉,需 ...

  9. Spring_之注解事务 @Transactional

    spring 事务注解 默认遇到throw new RuntimeException("...");会回滚需要捕获的throw new Exception("...&qu ...

随机推荐

  1. 「快速学习系列」我熬夜整理了Vue3.x响应性API

    前言 Vue3.x正式版发布已经快半年了,相信大家也多多少少也用Vue3.x开发过项目.那么,我们今天就整理下Vue3.x中的响应性API.响应性APIreactive 作用: 创建一个响应式数据. ...

  2. windows 2003系统安装

    一.使用workstation创建虚拟机 二.系统安装 点击"Enter" 点击"F8" 点击"Enter" 如下图所示: 点击" ...

  3. RPA应用场景-营业收入核对

    场景概述营业收入核对 所涉系统名称 SAP ,Excel,门店业务系统 人工操作(时间/次) 4 小时 所涉人工数量 6 操作频率每日 场景流程 1.每日13点起进入SAP查询前一日营业收入记账情况: ...

  4. arcgis创建postgre企业级数据库

    什么是企业级地理数据库? 企业级地理数据库(ArcSD Enterprise,sde)是和 arcGIS 套件集成程度最高的地理数据库:创建时需要用到安装 arcGIS Server 时的 [ecp ...

  5. Spring学习笔记(4)Spring 事件原理及其应用

    在 JDK 中已经提供相应的自定义事件发布功能的基础类: java.util.EventObject类 :自定义事件类型 java.util.EventListener接口:事件的监听器 首先了解几个 ...

  6. 2022-07-13 第六组 润土 Java01学习笔记

    1.数据类型: 基本数据类型: 整型: byte 字节型 -128-127 1个字节 short 短整型 2个字节 int 整型 4个字节 long 长整型 8个字节 浮点型: float 单精度 4 ...

  7. c# 添加指定扩展名的系统右键菜单(Windows11以前)

    在上篇文章c# 添加系统右键菜单(Windows11以前)中我们说了怎么在文件夹上增加一个菜单项,但是我们可能还需要给某个单独的扩展名添加右键菜单. 这里我们不用常见的扩展名来做,我们新做一个.jx的 ...

  8. Flink Window&Time 原理

    Flink 中可以使用一套 API 完成对有界数据集以及无界数据的统一处理,而无界数据集的处理一般会伴随着对某些固定时间间隔的数据聚合处理.比如:每五分钟统计一次系统活跃用户.每十秒更新热搜榜单等等 ...

  9. 第二十天python3 正则表达式

    正则表达式 Regular Expression,缩写为regex.regexp.RE等:分类 1.BRE 基本正则表达式,grep.sed.vi等软件支持,vim有扩展: 2.ERE 扩展正则表达式 ...

  10. 第一天python3 封装和解构

    封装 将多个值使用逗号分割,组合在一起:本质上,返回一个元组,只是省略了小括号:python特有语法,被很多语言学习和借鉴;比如javascript:t1=(1,2) 定义为元组:t2=1,2 将1和 ...