mysql 死锁 Deadlock found when trying to get lock; t

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

相关文章
相关标签/搜索