当前位置:网站首页>Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
2022-06-22 15:26:00 【InfoQ】

background
- commons-db : We maintain it internally , Is an annotation driven Spring Most resource management components in the ecosystem . Component to each DataSource Some default values for performance optimization are preset , Not all of them are listed , But it contains attributes that affect the direction of the problem (useLocalSessionState), as follows :
Properties defaultProperties = new Properties();
defaultProperties.put("prepStmtCacheSize", 300);
defaultProperties.put("prepStmtCacheSqlLimit", 2048);
defaultProperties.put("useLocalSessionState", true);
defaultProperties.put("cacheResultSetMetadata", true);
defaultProperties.put("elideSetAutoCommits", true);
- java-project : A project used to test component functionality , It will be used as a behavior test comparison with the project with problems .spring-boot:2.5.4、mysql-connector-java:8.0.26
- store: Game library project , It was this project that found the problem .spring-boot:2.6.6 、mysql-connector-java:8.0.28
- Alibaba cloud RDS (MySQL): Alibaba cloud MySQL The default isolation level is READ_COMMITTED, and MySQL The default isolation level is REPEATABLE_READ
problem
@Transactional
@DataSource(type = Type.MASTER,value = "developer")
public void addUser(ApolloUser user){
userRepository.save(user);
int i = 1/0; // Throw exceptions
}
- Specific performance: : perform addUser Method , When 1/0 Throw out RuntimeException When the type is abnormal ,user The object was added successfully . In one sentence ,【 Transaction rollback does not take effect 】.
hypothesis
- hypothesis 1: I have assumed whether it is @Transactional Of aop It didn't work , As a result, the explicit transaction is not opened .
- hypothesis 1 Don't set up , Because it's on debug After log mode , Clearly output the behavior log of each phase of the transaction , Such as :

- hypothesis 2: Considering the use of commons-db , If the framework layer connection management problem , This causes the transaction to start 、 The connections obtained during transaction rollback are inconsistent , It may also lead to this problem .
- hypothesis 2 Don't set up : It will be over soon , Because you can see from the above log that the connection is the same connection . And different connections perform unexpected opening 、 Rollback transactions should have exceptions .
Turning point
Turning point 1
Turning point 2
@Transactional(isolation = Isolation.REPEATABLE_READ)
@DataSource(type = Type.MASTER,value = "developer")
public void addUser(ApolloUser user){
userRepository.save(user);
int i = 1/0;
}
The first solution
Turning point 3

analysis
- 1、 I added @Transactional Before the execution of the method , Will execute the transaction manager (DataSourceTransactionManager) Of doBegin Method to create a transaction , stay doBegin In the method , Will be set autoCommit = false. It will determine whether the current isolation level is consistent with the user-defined , Otherwise, update the isolation level .

- 2、 After method execution fails , Will execute the transaction manager (DataSourceTransactionManager) Of doRollback Method to roll back the transaction .
Location problem
- When transaction rollback fails , The transaction flow is not executed SET autocommit=0 Instructions .


- useLocalSessionState: Maintain local sessionState , In need of judgment 【 Transaction commit mode 】、【 Isolation level 】 When setting , Get local status , Not every time MySQL Server Initiate an inquiry .
Decrypt
The second solution
useLocalSessionState=false
auto-commit=false
Explain why isolation Set to Isolation.REPEATABLE_READ Will take effect
public boolean isAutocommit() {
return (this.statusFlags & 2) != 0;
}
public boolean isAutoCommit() {
return this.autoCommit;
}
- Turning point 2: When the isolation level is set to REPEATABLE_READ when , Transaction rollback takes effect . Because when the user-defined isolation level RR And the default RC When not in agreement , Will trigger session Set a new isolation level , This will also statusFlags = 0 Updated to statusFlags = 2. Therefore, in the call isAutocommit () return true , It meets the requirements of execution SET autocommit=0 The conditions of the instruction .
Repetition problem
- upgrade spring-boot:2.6.6 Version and store bring into correspondence with : The question is repeated
- keep spring-boot:2.5.4, adjustment mysql-connector-java:8.0.28 : The problem is repeated
confirm bug
- https://github.com/mysql/mysql-connector-j

Repair
- 8.0.29 release:https://dev.mysql.com/doc/relnotes/connector-j/8.0/en/news-8-0-29.html
- A connection did not maintain the correct autocommit state when it was used in a pool with useLocalSessionState=true. (Bug #106435, Bug #33850099)
The final solution
Conclusion
边栏推荐
- Database connection pool: stress testing
- FreeRTOS task priority and interrupt priority
- "Forget to learn again" shell process control - 38. Introduction to while loop and until loop
- Sword finger offer46 -- translate numbers into strings
- Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world
- Reconstruction practice of complex C-end project of acquisition technology
- Vscode个性化设置:让一个小萌妹陪你敲代码
- [Software Engineering] design module
- [Software Engineering] planning and project management
- 数据库连接池:压力测试
猜你喜欢

数据资产管理:数据发现,发现什么,怎么发现?

C language student management system (open source)

Show me my personal work list for the past two years. I earn 6K a month in my spare time. It's so delicious to have a sideline

New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer

个人免签支付方案推荐

2022年失业的人多吗?今年是不是特别难找工作?

Simulation Keil et vspd

mysql的concat()函数如何用

口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?

mysql如何将字段修改为not null
随机推荐
难怪考研热度这么高,这些是研究生才有的“隐藏福利”!
Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
曾经,我同时兼职5份工作,只为给女友买个新款耳环......
嵌入式中的强符号和弱符号是什么?
ML笔记-matrix fundamental, Gradient Descent
Keil simulation and VSPD
Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
Vscode个性化设置:让一个小萌妹陪你敲代码
UE4 obtains local files through blueprints
同花顺开户难么?网上开户安全么?
SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
Method of using inout signal in Verilog
How to use the concat() function of MySQL
Flutter video Le lecteur écoute et joue automatiquement la prochaine chanson
FreeRTOS task priority and interrupt priority
A thorough understanding of singleton
Go all out to implement the flood control and disaster relief measures in detail and in place, and resolutely protect the safety of people's lives and property