Deadlock found when trying to get lock; try restarting transaction
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.html
相关推荐
[2017-02-10 13:12:06.678] [INFO] mysqlLog - update tbl_playerdata_error: { [Error: ER_LOCK_DEADLOCK: Deadlock found when trying to get lock; try restarting transaction] code: 'ER_LOCK_DEADLOCK', ...
当在更新表时遇到`DeadlockLoserDataAccessException`异常("Deadlock found when trying to get lock; try restarting transaction…"),意味着InnoDB检测到了一个死锁情况。这种异常不会直接影响用户的正常使用,...
摘要 今天来分享一下我在线上环境遇到的有关...Deadlock found when trying to get lock; try restarting transaction 定位问题 既然知道了是死锁造成的问题,那怎么定位问题呢?我们可以使用show engine innodb s
"mysql 死锁 Deadlock found when trying to get lock; try restarting transaction - hehaibo - ITeye技术网站.mht"这个文件可能包含有关如何识别和解决MySQL死锁的详细信息。解决死锁通常包括重新组织事务的顺序,...
- 错误返回:被回滚的事务会收到错误1213(Deadlock found when trying to get lock; try restarting transaction),提示开发者检查并解决可能导致死锁的代码逻辑。 了解了死锁的基本概念后,我们可以通过实际例子...
当出现"Deadlock found when trying to get to lock; try restarting transaction"这样的错误时,通常意味着有事务在等待其他事务释放资源,但双方都在等待对方先释放,从而形成僵局。 首先,我们需要了解死锁的...