当前位置:网站首页>@Several scenarios of transactional failure
@Several scenarios of transactional failure
2022-06-28 04:04:00 【Cloud on cloud】
Spring Of @Transactional Annotations control which scenarios the transaction does not take effect ?
1. The database engine does not support transactions ( only InnoDB Support )
from MySQL 5.5.5 The default storage engine to start with is :InnoDB, Before the default is :MyISAM, So it's worth noting , The underlying engine doesn't support transactions. No matter what happens, it's just a waste of time .
[email protected]Transactional Annotations can only be applied to public The method of visibility
If it is to be used in non public On the way , Can be opened AspectJ The proxy pattern .
The following is from Spring Official documents :
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
3..try-catch abnormal , The transaction will not take effect
It's also the most common , Eat the abnormal , And then don't throw it out , The transaction will not roll back !
spring The transaction of is started before calling the business method , The business method is executed after execution commit or rollback,
Whether a transaction is executed depends on whether it is thrown runtime abnormal . If you throw runtime exception And there's no... In your business approach catch When you arrive , The transaction rolls back .
This situation can be used @Transactional(rollbackFor = Exception.class) Processing or reprocessing catch Manually execute rollback in

4. By default ,Spring Would be right Error perhaps RuntimeException Abnormal transaction rollback ,
Others inherit from java.lang.Exception It's abnormal : Such as IOException、TimeoutException etc. , No rollback .
Solution :Transactional Annotation plus rollbackFor attribute , Appoint java.lang.Exception.class;
5. Method calls in the same class , Lead to @Transactional invalid
scene : In development, it is inevitable to call methods in the same class , For example, there is a class Test, One way of it A,A Call the methods of this class B
( No matter how B Yes, it is public still private modification ), But the way A There is no declaration to annotate the transaction , and B There are methods . Then the external call method A after ,
Method B It doesn't work .
reason : Due to the use Spring AOP The agent caused , Because only when the transaction method is called by code other than the current class , Only by Spring Generated proxy objects to manage .
边栏推荐
猜你喜欢

视频爆炸时代,谁在支撑视频生态网高速运行?

Web APIs DOM event foundation dark horse programmer

从遇见大咖到成为大咖,昇腾AI开发者创享日给开发者带来无限可能

02 MongoDB数据类型、重要概念以及shell常用指令

Detailed explanation of KVM common commands

django. core. exceptions. ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"

Particle theory of light (photoelectric effect / Compton effect)

使用信号分析器

解析STEAM教育框架下未来教师研究能力
随机推荐
leetcode - 329. 矩阵中的最长递增路径
Notes to friendship chain
Detailed explanation of KVM common commands
从遇见大咖到成为大咖,昇腾AI开发者创享日给开发者带来无限可能
数字有为,易步到位 华为携“5极”明星产品加速布局商业市场
leetcode:494.数组中添加加减运算符得到指定值的所有方法
MySQL 主从复制、分离解析
04 MongoDB各种查询操作 以及聚合操作总结
电学基础知识整理(一)
Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
How to write a software test report? Here comes the third party performance report template
基于arm5718的Shell脚本参数传递的2种方法
Unity C# 网络学习(十一)——自定义协议生成工具
加法器—笔记
利用ELK 搭建日志分析系统(一)—— 组件介绍
多线程与高并发四:VarHandle与强软弱虚引用和ThreadLocal
软件测试报告怎么编写?第三方性能报告范文模板来了
Staggered and permutation combination formula
2021 year end summary and 2022 outlook
ELK 搭建日志分析系统 + Zipkin服务链路追踪整合