MySQL return Deadlock with insert row and FK is locked 'for update'
Posted
by constantin-slednev
on Stack Overflow
See other posts from Stack Overflow
or by constantin-slednev
Published on 2010-04-01T11:47:47Z
Indexed on
2010/04/01
11:53 UTC
Read the original article
Hit count: 420
Hello developers!
I get deadlock error in my mysql transaction.
The simple example of my situation:
Thread1 > set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
Thread1 > SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
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 > set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
Thread2 > SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Query OK, 0 rows affected (0.00 sec)
Thread2 > INSERT INTO B (AID, NAME) VALUES (1000, 'Hello world');
SLEEP
Query OK, 1 row affected (4.99 sec)
Thread1 > INSERT INTO B (AID, NAME) VALUES (1000, 'Hello world2');
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
B.AID -> FK -> 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
Can you give me more solutions ? Current solutions do not fit me.
© Stack Overflow or respective owner