Deadlock found when trying to get lock; try restarting transactionhtml
1213 - Deadlock found when trying to get lock; try restarting transaction
出現這個緣由要記住一點就是:innodb的行鎖 和解鎖都是針對主鍵索引的。若是查詢時根據索引鎖表,但更新時卻不是經過主鍵更新,
那麼等待的解鎖查詢的進程將會報1213錯誤,程序裏有可能返回一個null值
實例:
table
soldgoods (表名)
soldgoodsID 索引
productid
businessid
開啓線程A
執行:
set autocommit=0;
select businessid from soldgoods where soldgoodsID = 'ac63837c76222e4a5419e2529d775ae4' for UPDATE;
查詢得過結果
開啓線程B
執行:
set autocommit=0;
select businessid from soldgoods where soldgoodsID = 'ac63837c76222e4a5419e2529d775ae4' for UPDATE;
查詢等待解鎖
這個時候在線程A中執行:
update soldgoods set productid = 2 where businessid = '0a527df4763c3dc71cbafebec5a8d787'
不是根據主鍵而去更新鎖表的值
線程B會出現:
[Err] 1213 - Deadlock found when trying to get lock; try restarting transaction
若是將最後線程A中執行的語句改變:
update soldgoods set productid = 2 where soldgoodsID = 'ac63837c76222e4a5419e2529d775ae4'
根據索引修改值
而後
commit;
提交事務。線程B就能順利獲得查詢值了線程
轉載自:http://blog.sina.com.cn/s/blog_4acbd39c01014gsq.html3d