Deadlock found when trying to get lock; try restarting transaction
1213 – Deadlock found when trying to get lock; Try restarting transaction
one thing to remember is that the row lock and unlock of InnoDB are for primary key index. If the table is locked according to the index when querying, but not through the primary key when updating,
the process waiting to unlock the query will report 1213 error, and the program may return a null value
instance:
Table
solidgoods (table name)
solidgoodsid index
productid
businessid
start thread a
execution:
set autocommit = 0
select businessid from soldgoods where soldgoodsID = ‘ac63837c76222e4a5419e2529d775ae4’ for UPDATE;<
query result
start thread B
execute:
set autocommit = 0
select businessid from soldgoods where soldgoodsID = ‘ac63837c76222e4a5419e2529d775ae4’ for UPDATE;<
the query is waiting to be unlocked
at this time, it is executed in thread a:
update solidgoods set productid = 2 where business id =’0a527df4763c3dc71cbafebec5a8d787 ‘
the value of the lock table is not updated according to the primary key
thread B will appear:
[err] 1213 – deadlock found when trying to get lock; Try restarting transaction
if the statement executed in the last thread a is changed:
update soldgoods set productid = 2 where soldgoods id =’ac63837c76222e4a5419e2529d775ae4 ‘
modify the value according to the index
and then
commit
commit the transaction. Thread B can get the query value smoothly