Hello developers!
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:
- catch deadlock error in code and retry query.
- use innodb_locks_unsafe_for_binlog in my.cnf
- lock (for update) table A in Thread2 before insert
Is there any other solutions ?