Spring_事务总结
Spring 事务总结
rollbackFor 设为 Exception.class场景下
如果在函数内部catch住异常消费掉,没有再抛出的话,不会回滚
如果catch住 然后原封不动抛出,会回滚
如果catch住,然后改造成其他异常抛出,会回滚
如果是内层函数抛出,外层带事务的函数未抛出,也不会回滚
如果外层带事务函数catch住再抛出,会回滚
事务函数调用本类的public带有事务的函数,第二个函数不会带有事务,相当于一个普通函数,除非是调用其他类的事务函数
如果是@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) && 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) && execution(* *(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
</beans>
最后建议还是使用注解的方式,更加明白直接,因为拦截器有可能由漏网之鱼,问题难以排查
Spring_事务总结的更多相关文章
- Spring_事务管理
转自:https://www.ibm.com/developerworks/cn/java/j-master-spring-transactional-use/index.html 事务管理是应用系统 ...
- Spring_事务-注解代码
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...
- Spring_事务
事务管理: 用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当做一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性 原子性 一致性 隔离性 持久性 Spring两 ...
- Spring_事务(2)
- Spring_事务(1)
- Spring_事务准备
- spring 课程
官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // ...
- Spring_使用XML文件的方式配置事务
步骤: 正常配置jdbctemplate 正常配置bean 配置事物管理器 配置事物管理器 配置aop切入点,通过切入点把事物链接起来 思路: 接着上一个买书的例子吧,直接拷到新包下,把注解都干掉,需 ...
- Spring_之注解事务 @Transactional
spring 事务注解 默认遇到throw new RuntimeException("...");会回滚需要捕获的throw new Exception("...&qu ...
随机推荐
- python基础知识-day8(函数实战)
1 def out(): 2 username=input("请输入用户名:\n") 3 password=input("请输入密码:\n") 4 return ...
- Linux字符集和编码
计算机内部,所有信息最终都是一个二进制值形式存放 字符集 字符集:charset是character set的简写,即二进制和字符的对应关系,不关注最终的存储形式 编码 字符集编码:encoding是 ...
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
开发经常遇到分页查询的需求,但是当翻页过多的时候,就会产生深分页,导致查询效率急剧下降. 有没有什么办法,能解决深分页的问题呢? 本文总结了三种优化方案,查询效率直接提升10倍,一起学习一下. 1. ...
- 深度学习基础-基于Numpy的感知机Perception构建和训练
1. 感知机模型 感知机Perception是一个线性的分类器,其只适用于线性可分的数据. f(x) = sign(w.x + b) 其试图在所有线性可分超平面构成的假设空间中找 ...
- RS485通信电路
RS485由RS232和RS422发展而来,弥补了抗干扰能力差.通信距离短.速率低的缺点,增加了多点.双向通信能力,即允许多个发送器连接在同一条主线上,同时增加了发送器的驱动能力和冲突保护特性,扩展了 ...
- HTML基础学习笔记(一)
简介 基本形式 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- 利用Kaptcha.jar生成图片验证码(以下源码可以直接复制并自定义修改)
说明:Kaptcha是一个很实用的验证码生成工具,它可以生成各种样式的验证码,因为它是可以配置的 目录: 一 实现步骤 二 实例 A 编写jsp页面 B 配置web.xml C 验证输入正确与否. 一 ...
- Java基础语法02
回顾前面的章节,我们学习了(1.注释,2.标识符和关键字,3.数据类型)今天让我们继续加油. 四.变量,常量,作用域1.变量是什么:存数的(可以变化的量) Java是一种强类型语言,每个变量都必须声明 ...
- 解决 Vue 部署在域名子路由 问题
我们先看下官方说明 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上,例如 https://www.my-app.com/ .如果应用被部署在一个子路径上,你就需要用这个选项指定 ...
- JDBC与ODBC的区别
JDBC简介JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,它是Java十三个规范之一.可以为多种关系数据库提供统一访 ...