当前位置:网站首页>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
边栏推荐
- [10. difference]
- [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,
- 关于el-date-picker点击清空参数变为null的问题
- 准备好迁移上云了?请收下这份迁移步骤清单
- Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
- Chapter 11 signal (I) - concept
- IMX8QXP DMA资源和使用(未完结)
- MATLAB小技巧(18)矩阵分析--熵权法
- 针对直播痛点的关键技术解析——首帧秒开、清晰度、流畅度
- [c++ primer notes] Chapter 4 expression
猜你喜欢
随机推荐
Install Jenkins
2022 love analysis · panoramic report of it operation and maintenance manufacturers
Blind survey shows that female code farmers are better than male code farmers
How much do you know about the cause of amplifier distortion?
Rust async: SMOL source code analysis -executor
2022.06.26(LC_6100_统计放置房子的方式数)
Redis主从复制以及哨兵模式
MATLAB小技巧(19)矩阵分析--主成分分析
Lvgl usage demo and instructions 2
直接修改/etc/crontab 文件内容,定时任务不生效
[ 扩散模型(Diffusion Model) ]
SIG associé: toutes les routes mènent à ue5
MySQL环境变量配置的教程
Etcd tutorial - Chapter 5 etcd etcdctl usage
PayPal账户遭大规模冻结!跨境卖家如何自救?
参考 | 升级 Win11 移动热点开不了或者开了连不上
[batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of
RMAN-08137 主库无法删除归档文件
Redis的事务
静态代码块Vs构造代码块

![[11. two dimensional difference]](/img/b2/da624f8a7f97c46b8e346cf6d6da49.png)







