当前位置:网站首页>I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
2022-06-27 08:30:00 【InfoQ】
One 、 Preface : One Bug
I didn't expect one Bug, I was fucked twice !
- This is this. selectKey Configuration of , In the execution of the insert SQL after , Start to get the last index value .
- Usually as long as the configuration is OK , The return object also has a corresponding id Field , Then you can get the return value correctly .PS: Here's the problem , Written by little brother fu Mybatis It turns out that only one 0!
Two 、 analysis : Abnormal diagnosis

- Mybatis The process of processing can be divided into two parts , One part is parsing , The other part is to use . When parsing, put Mapper XML Medium insert Label statements are parsed out , At the same time, analyze selectKey label . After the final parsing , Use the parsed statement information MappedStatement The mapping statement class is stored . Convenient for follow-up in DefaultSqlSession When performing an operation , It can be downloaded from Configuration It is obtained from the configuration item for use .
- So here is a very important point , Is to perform insert When inserting , It also contains a query operation . That means , We'll be at once Insert in , Contains two execution statements .a key:bug It happened here , Why? ? Because when these two statements are executed at the beginning , When getting links , Each one is to get a new link , So in other words ,insert xxx、select LAST_INSERT_ID() In two connection During connection execution , It's not true , Unable to get the inserted index ID, Only under a link or a transaction ( once commit) Can have the characteristics of transaction , Get the auto increment after inserting data ID.
- And because this part was written at the beginning JdbcTransaction Realization Transaction When the interface gets the connection , Every time it's a new link , The code block is as follows ;

- Here's a link to get , At the beginning, there was no if null The judgment of the , Every time I get the link directly , So this is not a link under the two SQL operation , So we will not get the right result , It is equivalent to just executing alone
SELECT LAST_INSERT_ID()So the final query result is 0 When it's over !You can test copying this statement to SQL Query tool
3、 ... and 、 shock : The same pit

Four 、 common : Transaction failure
- The database engine does not support transactions : Here we use MySQL For example , Its MyISAM The engine does not support transaction operations ,InnoDB It's the engine that supports transactions , Generally, support transactions will use InnoDB.https://dev.mysql.com/doc/refman/8.0/en/storage-en...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 .
- The method is not public Of : come 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.】@Transactional It can only be used for public On the way , Otherwise, the transaction will not fail , If it is to be used in non public On the way , Can be opened AspectJ The proxy pattern .
- Has not been Spring management :
// @Service - It is commented out here public class OrderServiceImpl implements OrderService { @Transactional public void placeOrder(Order order) { // ... } }
- The data source is not configured with transaction manager : Generally, it comes from the self-developed database routing component
@Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }
- Abnormally swallowed .catch Then I ate it directly , Transaction exception cannot be rolled back . At the same time, configure the corresponding exception
@Transactional(rollbackFor = Exception.class)
5、 ... and 、 summary : Learning experience
边栏推荐
- ZABBIX deployment instructions (server+win client + switch (H3C))
- 三道基础面试题总结
- 力扣84柱状图中最大的矩形
- 2022.6.26-----leetcode.710
- 2022 love analysis · panoramic report of it operation and maintenance manufacturers
- MySQL环境变量配置的教程
- Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
- 数字IC-1.9 吃透通信协议中状态机的代码编写套路
- oracle怎样将字符串转为多行
- Pin details in rust
猜你喜欢

MATLAB小技巧(18)矩阵分析--熵权法

我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
![[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,](/img/d1/b7e70e6f0be8e128f38afbe7ac927c.png)
[batch dos-cmd command - summary and summary] - how to distinguish the internal command and external command of CMD, and the difference between CMD command and run (win+r) command,

RMAN-08137 主库无法删除归档文件

(原创)自定义Drawable

"Short video" Linxia fire rescue detachment carries out fire safety training

Ue5 magic power - POI solution

准备好迁移上云了?请收下这份迁移步骤清单

ServletConfig与ServletContext
![[10. difference]](/img/15/ffd93da75858943fe887de1718e0f6.png)
[10. difference]
随机推荐
Linux下Redis的安装
【论文阅读】Intrinsically semi-supervised methods
[paper reading] internally semi supervised methods
SQL Sever column name or number of supplied values does not match the table definition
(原创)自定义Drawable
SPARQL基础入门练习
[11. two dimensional difference]
LVGL GUI GUIDER移植代码到STM32
关于el-date-picker点击清空参数变为null的问题
How much do you know about the cause of amplifier distortion?
数据类型占内存大小?LongVsObject
Correctly understand MySQL mvcc
【每日一练】产品卡片动画效果的实现
若xn>0,且x(n+1)/xn>1-1/n(n=1,2,...),证明级数∑xn发散
IMX8QXP DMA资源和使用(未完结)
Game asset reuse: a new way to find required game assets faster
That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
关于放大器失真的原因你了解多少呢?
参考 | Win11 开启热点之后电脑不能上网