Can two or more instances of the following deadlock with one another?
START TRANSACTION
SELECT mycolumn FROM mytable WHERE myid IN (list-of-ids) FOR UPDATE
:
COMMIT
Points to keep in mind:
- mycolumn is indexed
- This is mariaDB/innoDB
- There is only one table involved (
mytable)
Example A:
Thread 1:
START TRANSACTION
SELECT mycolumn FROM mytable WHERE myid IN (100,200) FOR UPDATE
:
COMMIT
Thread 2: (note that IN-list is in opposite order)
START TRANSACTION
SELECT mycolumn FROM mytable WHERE myid IN (200,100) FOR UPDATE
:
COMMIT
Could these threads possibly deadlock on the SELECT in mariaDB/innoDB?
Example B:
Thread 1:
START TRANSACTION
SELECT mycolumn FROM mytable WHERE myid IN (100,200) FOR UPDATE
:
COMMIT
Thread 2: (note that IN-list is in same order)
START TRANSACTION
SELECT mycolumn FROM mytable WHERE myid IN (100,200) FOR UPDATE
:
COMMIT
Could these threads possibly deadlock on the SELECT in mariaDB/innoDB?
UPDATE: A traditional way of avoiding deadlocks is to be sure to acquire locks in the same order, so that you can't have Thread 1 owning lock A and waiting on lock B while Thread 2 owns lock B and is waiting on lock A. However, I've seen several posts suggesting that even getting a single lock can't be considered atomic, because there are asynchronous processes going on in the engine that could still result in deadlock (and as a corollary to this, advising developers to always expect deadlocks and recover from them by retrying the transaction). I'm really asking whether deadlock can be eliminated by using proper coding practices in innoDB.)