查看status日志發(fā)現(xiàn)兩條insert 出現(xiàn)了死鎖 RECORD LOCKS space id 388 page no 27032 n bits 616 index `idx_svcorderserviceitem_workorderid_quantity` of table `ecejservice`.`svc_order_service_item` trx id 596252578 lock_mode X insert intention waiting 可以確定,這個x鎖不是由于INSERT產(chǎn)生的,因為 INSERT可能產(chǎn)生的鎖包括檢查dup key時的s鎖,隱式鎖轉(zhuǎn)換為顯式鎖(not gap,要在二級索引上產(chǎn)生lock_mode為X的LOCK_ORDINARY類型的鎖(包括記錄及記錄前面的gap),據(jù)我所知一般是根據(jù)二級索引掃描進行記錄更新導致的。
session2: mysql> select * from test01 where app='08' for update; --第二步 鎖住【12,07】-【13,08】以及【13,08】-【14,09】兩段區(qū)間 +----+-----+ | id | app | +----+-----+ | 13 | 08 | +----+-----+ 1 row in set (0.00 sec)
mysql> insert into test01(app) values ('04'); ----第四步 等待第一步釋放,,于是死鎖 ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
*** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 64 page no 4 n bits 80 index idx_app of table `devops`.`test01` trx id 5376 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 6 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 2; hex 3035; asc 05;; 1: len 4; hex 00000007; asc ;;