I get deadlock error in my mysql transaction.
The simple example of my situation:
Thread1 > BEGIN;
Query OK, 0 rows affected (0.00 sec)
Thread1 > SELECT * FROM A WHERE ID=1000 FOR UPDATE;
1 row in set (0.00 sec)
Thread2 > BEGIN;
Query OK, 0 rows affected (0.00 sec)
Thread2 > INSERT INTO B (AID, NAME) VALUES (1000, 'Hello world');
[Hangs]
Thread1 > INSERT INTO B (AID, NAME) VALUES (1000, 'Hello world2');
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
Thread2 >
Query OK, 1 row affected (10.00 sec)
B.AID is a FOREIGN KEY referring to A.ID
I see three solutions:
Is there any other solutions ?
I don't know what code surounds this examples, but it might be worth using LOCK IN SHARE MODE
for both Threads, since you're not actually updating the row itself. If you must use LOCK FOR UPDATE
, I would think that locking the other thread would be the only logical path.
Also if you open to moving away from MySQL, I've found that PostgreSQL has much better resolution of deadlocks. In some cases, I was finding MySQL deadlocked every time when running the same script on >1 thread. Where the same script in PostgreSQL could handle it just fine for any number of parallel threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With