baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.36k stars 4.31k forks source link

多线程下复用LambdaUpdateWrapper 导致全表更新 #5762

Closed ChiMuYuan closed 11 months ago

ChiMuYuan commented 11 months ago

当前使用版本(必填,否则不予处理)

4.1.3 3.5.3.2 3.5.3.2 3.0.2 Mysql == 8.0.28 ### 该问题是如何引起的?(确定最新版也有问题再提!!!) 造数据时偶然发现,多线程并发复用LambdaUpdateWrapper导致全表数据更改 没找到相关说明 未验证模拟生产多用户同时请求结果,类似pv、uv的操作,此结果是一个请求开线程池的结果,搬砖任务繁重......没有继续研究下去了也没有翻源码 本人实际生产不是这么写的,测试造数据有类似写法...提出来看看怎么回事避个雷 ### 重现步骤(如果有就写完整) value = value+1 只更新id=1 线程池并发 关键代码: ``` @GetMapping("/test") public void test() { ExecutorService executorService = Executors.newFixedThreadPool(5); LambdaUpdateWrapper testTableLUW = new LambdaUpdateWrapper<>(); testTableLUW.setSql("value = value + 1") .eq(TestTable::getId, 1L); for (int i = 0; i < 5; i++) { executorService.submit(() -> { for (int j = 0; j < 100; j++) { testTableService.update(testTableLUW); } }); } } ``` ``` @TableName("test_table") @Schema(name = "TestTable对象", description = "") public class TestTable implements Serializable { private static final long serialVersionUID = 1L; @Schema(description = "主键") @TableId("id") private Long id; @JSONField(serialzeFeatures = SerializerFeature.WriteEnumUsingToString) @TableField("value") private CustomerStatusEnum value; ``` ### 报错信息 执行前数据库: ![image](https://github.com/baomidou/mybatis-plus/assets/26971626/0aa9ff62-e2b9-4ad6-90df-00e76e5fc6f4) 执行后: ![image](https://github.com/baomidou/mybatis-plus/assets/26971626/a74ea131-dccf-4916-8b20-003b93fa97a3)
miemieYaho commented 11 months ago

不支持多线程

ChiMuYuan commented 11 months ago

@miemieYaho 感觉可以在警告提一嘴... image