Message: Deadlock found when trying to get lock; try restarting transaction
InnoDB reports
this error when a transaction encounters a deadlock and is automatically rolled back so that your application can take corrective action. To recover from this error, run all the operations in this transaction again. A deadlock occurs when requests for locks arrive in inconsistent order between transactions. The transaction that was rolled back>
上面有两句:
To recover from this error, run all the operations in this transaction again If you encounter frequent deadlocks, make the sequence of locking operations (LOCK TABLES, SELECT ... FOR UPDATE, and so on)
consistent between the different transactions or applications that experience the issue
One easy trick that can help with most deadlocks is sorting the operations in a specific order.
You
get a deadlock when two transactions are trying to lock two locks at opposite orders, ie:
connection
1: locks key(1), locks key(2);
connection
2: locks key(2), locks key(1);
If both run at the same time, connection
1 will lock key(1), connection 2 will lock key(2) and each connection will wait for the other to>
Now,
if you changed your queries such that the connections would lock the keys at the same order, ie:
connection
1: locks key(1), locks key(2);
connection
2: locks key(1), locks key(2);
it will be impossible to
get a deadlock.
So
this is what I suggest:
Make sure you have no other queries that
lock access more than one key at a time except for the delete statement. if you do (and I suspect you do), order their WHERE in (k1,k2,..kn) in ascending order.
Fix your delete statement to work
in ascending order:
Change
DELETE FROM onlineusers WHERE datetime
<= now() - INTERVAL 900 SECOND
To
DELETE FROM onlineusers WHERE> WHERE datetime
<= now() - INTERVAL 900 SECOND order by>
Another thing to keep
in mind is that mysql documentation suggest that in case of a deadlock the client should retry automatically. you can add this logic to your client code. (Say, 3 retries on this particular error before giving up).