[用事實說明兩個凡是]一個mysql莫名鎖表的問題

背景

上回書<<一個由mysql事務隔離級別形成的問題分析>>說到, 有兩個實例去更新一條記錄, 一條成功, 一條失敗. 成功的認爲本身獲取到的記錄, 因而 開開心心的把任務進行處理, 可是在他試圖將任務狀態更新爲處理成功時, 去發現任務被別人鎖了...php

具體問題

搶到任務的進程css

2015-11-23 19:42:01|INFO|exec_task.php|40||get one task: 11
...
//開始處理
2015-11-23 19:42:01|INFO|exec_task.php|107||line_count: 9
2015-11-23 19:42:01|INFO|exec_task.php|147||fork child success: 8346
2015-11-23 19:42:01|INFO|exec_task.php|264||[0] pid: 8346, start: 0, stop: 0

2015-11-23 19:42:01|INFO|exec_task.php|147||fork child success: 8347
2015-11-23 19:42:01|INFO|exec_task.php|264||[1] pid: 8347, start: 1, stop: 1

2015-11-23 19:42:01|INFO|exec_task.php|147||fork child success: 8348
2015-11-23 19:42:01|INFO|exec_task.php|264||[2] pid: 8348, start: 2, stop: 2

2015-11-23 19:42:01|INFO|exec_task.php|147||fork child success: 8349
2015-11-23 19:42:01|INFO|exec_task.php|264||[3] pid: 8349, start: 3, stop: 3

2015-11-23 19:42:01|INFO|exec_task.php|147||fork child success: 8350
2015-11-23 19:42:01|INFO|exec_task.php|264||[4] pid: 8350, start: 4, stop: 4
...
2015-11-23 19:42:01|INFO|exec_task.php|159||child procss exit ,start to merge result file
//處理結束,更新狀態,結果發現等待鎖超時
2015-11-23 19:42:52|ERR|function.inc.php|113||SQL fail: Lock wait timeout exceeded; try restarting transaction

沒有搶到任務的進程mysql

2015-11-23 19:42:51|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:51|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
....
2015-11-23 19:42:52|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:53|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:53|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:53|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task
2015-11-23 19:42:53|INFO|function.inc.php|100||task_id 11 is locked by another process, get next task

即因爲沒有搶到任務的進程在2015-11-23 19:42:52的時候,正在死循環, 事務沒有提交, 把記錄給鎖了, 因此搶到任務的進程 想把記錄更新的時候, 出現等待鎖超時.sql

一個事務沒有提交,把記錄鎖了, 致使另外一個事務沒法更新, 聽起來很正常對吧.rest

請注意, 沒有搶到任務的進程並無更新到task_id = 11的記錄,卻把這個記錄鎖了.code

問題重現

這個問題其實很好重現. 見如下操做記錄:進程

表結構事務

create database if not exists ae;
create table ae.task (
id int primary key,
status int);

操做記錄1 操做記錄2

問題綜述

在兩個事務同時更新同一條記錄的狀況下, 一個事務因另外一個事務將記錄修改致使的條件不知足而更新不成功, 更新不成功的事務仍是會把這條記錄鎖住.get

相關文章
相關標籤/搜索