3

解决mysql死锁问题 SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceede...

 9 months ago
source link: https://www.cnblogs.com/tinywan/p/17874955.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

钉钉机器人报警了

1021265-20231204145856708-1306016151.png
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

 在PHP调试时 提交事务触发异常后没有执行回滚导致mysql死锁,以致后续请求更新不了数据

在mysql中事务a执行修改数据,比如: update table set a=1 where id=1;此时事务并未进行提交也没有回滚,然后事务B开始运行,修改同一条数据: update table set a=2 where id=1;

问题出现环境

1、在同一事务内先后对同一条数据进行插入和更新操作;
2、多台服务器操作同一数据库;
3、瞬时出现高并发现象;
4、语句中没有执行commit,也没有rollback就return退出了
比如参数检查不通过,直接return错误信息,导致回滚不能执行
如以下代码先执行了更新操作,后面出错又直接返回,导致没有执行rollback,对于这种操作return前一点要回滚,或者抛出异常统一扑捉后返回错误信息
$this->startTrans();
try {
   $user = new User();
   $user->where('id',$userId)->update(['realname'=>$parentName]);
   $existId = $this->where('class_id',$classId)->where('student_number',$number)->find();
   if ($existId) {
      return ['data' => '', 'code' => 300, 'msg' => '学号重复'];
   }
   $this->commit();
} catch (Throwable $e) {
   $this->rollback();
   return ['data' => '', 'code' => 20102, 'msg' => $e->getMessage()];
}
1021265-20231204165003672-732798094.png

更多:https://blog.51cto.com/u_12390904/6254246


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK