解析Bug #382
v3.0: Duplicate index_total and ca_index in order lists
Start date:
Due date:
% Done:
0%
Estimated time:
8.00 h
Description
Task Details
- 查找重复的原因已经解决方案
- 原因:因该系统在并发方面的处理不够完善,导致2人同时下单时,都获取到同一个total_index和ca_index
- 方案:添加锁机制(也许可以使用文件锁?或者数据库锁)在并发下单时,通过锁机制避免使用相同的index。
- 数据库锁相关内容补充:使用行锁:select ... for update, 以下是个demo
<?php // 连接数据库 $pdo = new PDO("mysql:host=localhost;dbname=your_database", "username", "password"); try { // 开始事务 $pdo->beginTransaction(); // 获取最大键值并加锁 $stmt = $pdo->prepare("SELECT MAX(key) AS max_key FROM order_table FOR UPDATE"); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $maxKey = $row['max_key']; // 生成新订单的键值 $newKey = $maxKey + 1; // 插入新订单 $stmt = $pdo->prepare("INSERT INTO order_table (key, ...) VALUES (?, ...)"); $stmt->execute([$newKey, ...]); // 插入其他订单信息 // 提交事务 $pdo->commit(); echo "订单生成成功!新订单的键值为:" . $newKey; } catch (Exception $e) { // 出现异常时回滚事务 $pdo->rollBack(); echo "订单生成失败:" . $e->getMessage(); } ?>
- 数据库锁相关内容补充:使用行锁:select ... for update, 以下是个demo
- QQ from sales staff
- Report and Solution
Others