当前位置:网站首页>Failure scenarios of @Transactional annotations
Failure scenarios of @Transactional annotations
2022-07-31 07:58:00 【talking pikachu】
在开发过程中,We encounter things all the time,为了解决这个问题,springProvides a good transaction management mechanism,
javaThe development process is mainly divided into two categories
编程式事务和声明式事务
A programmatic transaction is the act of manually transacting while writing code,代码侵入性较高
Regarding specific operations, it is mainly possible to pass
transactionManager.commit
以及
transactionManager.rollback
进行操作
The other is declarative programming
@Transactional就是一个很好的例子
@Transactional 可以作用在接口、类、类方法
It's easier to understand if it works on a class,That's all for that classpublicThe methods all guarantee the same transaction attribute information,作用于,It's also easy to understand that it works on methods,But if the class is configured @Transactional The method is also configured at the same time @Transactional 的话,At a fine-grained method level @Transactional 配置为准
It is not recommended to act on interfaces,因为一旦标注在Interface上并且配置了Spring AOP 使用CGLib动态代理,将会导致@Transactional注解失效
什么是CGLib呢
传统的jdk动态代理与CGLibThe proxy difference isjdkBoth the proxy class and the target class of a dynamic proxy inherit the same interface,This makes them want to实现相同的接口,那么代理类和目标类的方法名就一样了
CGLibDynamic proxies are proxy classes to go继承目标类,然后重写其中目标类的方法,这样也可以保证代理类拥有目标类的同名方法;
代理类去继承目标类,每次调用代理类的方法都会被方法拦截器拦截,在拦截器中才是调用目标类的该方法的逻辑
A little off topic,keep learning
关于@Transactional的各种属性:
propagation属性
propagation 代表事务的传播行为,默认值为
Propagation.REQUIRED,其他的属性信息如下:
Propagation.REQUIRED:如果当前存在事务,则加入该事务,如果当前不存在事务,则创建一个新的事务.(
也就是说如果A方法和B方法都添加了注解,在默认传播模式下,A方法内部调用B方法,会把两个方法的事务合并为一个事务 )
Propagation.SUPPORTS:如果当前存在事务,则加入该事务;如果当前不存在事务,则以非事务的方式继续运行.
Propagation.MANDATORY:如果当前存在事务,则加入该事务;如果当前不存在事务,则抛出异常.
Propagation.REQUIRES_NEW:重新创建一个新的事务,如果当前存在事务,暂停当前的事务.( 当类A中的 a
方法用默认Propagation.REQUIRED模式,类B中的 b方法加上采用
Propagation.REQUIRES_NEW模式,然后在 a 方法中调用 b方法操作数据库,然而
a方法抛出异常后,b方法并没有进行回滚,因为Propagation.REQUIRES_NEW会暂停 a方法的事务 )
Propagation.NOT_SUPPORTED:以非事务的方式运行,如果当前存在事务,暂停当前的事务.
Propagation.NEVER:以非事务的方式运行,如果当前存在事务,则抛出异常.
Propagation.NESTED :和 Propagation.REQUIRED 效果一样.
isolation 属性
isolation :事务的隔离级别,默认值为 Isolation.DEFAULT.
Isolation.DEFAULT:使用底层数据库默认的隔离级别. Isolation.READ_UNCOMMITTED
Isolation.READ_COMMITTED Isolation.REPEATABLE_READ
Isolation.SERIALIZABLEtimeout 属性
timeout :事务的超时时间,默认值为 -1.如果超过该时间限制但事务还没有完成,则自动回滚事务.
readOnly 属性
readOnly :指定事务是否为只读事务,默认值为
false;为了忽略那些不需要事务的方法,比如读取数据,可以设置 read-only 为 true.
rollbackFor 属性
rollbackFor :用于指定能够触发事务回滚的异常类型,可以指定多个异常类型.
noRollbackFor属性
noRollbackFor:抛出指定的异常类型,不回滚事务,也可以指定多个异常类型.
@Transactional失效场景:
如果Transactional注解应用在非public 修饰的方法上,会失效
之所以会失效是因为在Spring AOP 代理时,TransactionInterceptor (事务拦截器)在目标方法执行前后进行拦截,会间接调用 AbstractFallbackTransactionAttributeSource的 computeTransactionAttribute 方法,获取Transactional 注解的事务配置信息.
protected TransactionAttribute computeTransactionAttribute(Method method,
Class<?> targetClass) {
// Don't allow no-public methods as required.
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
return null;
}
@Transactional 注解属性 propagation 设置错误
@Transactional 注解属性 rollbackFor 设置错误
同一个类中方法调用,导致@Transactional失效
异常被你的 catch“吃了”导致@Transactional失效:
spring的事务是在调用业务方法之前开始的,业务方法执行完毕之后才执行commit or rollback,事务是否执行取决于是否抛出runtime异常.如果抛出runtime exception 并在你的业务方法中没有catch到的话,事务会回滚.
数据库引擎不支持事务
边栏推荐
猜你喜欢
‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
任务及任务切换
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
SCI写作指南
2022.07.29_每日一题
Embedded system driver primary [2] - _ parameters and dependencies under the kernel module
金融租赁业务
进程调度的基本过程
[PSQL] SQL基础教程读书笔记(Chapter1-4)
【Go】Go 语言切片(Slice)
随机推荐
客户端navicat安装教程
PCB抄板
基于LSTM的诗词生成
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
页面懒加载
2022.07.15_每日一题
正则表达式绕过
R语言 第一部分
2022.7.29 Array
中断及pendSV
【Go语言入门教程】Go语言简介
[PSQL] SQL基础教程读书笔记(Chapter1-4)
Chapter 9 Exceptions try...except...else...finally
2022.07.29_Daily Question
Titanic 预测问题
我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
mysql插入新字段方法
基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
MySQL table creation statement_Three commonly used MySQL table creation statements
《c语言小游戏》入门级三子棋游戏(机器人加强版)