I came across the situation where deadlock was found while inserting the records in the child table.
Let me explain the scenario in detail
I am having two tables
1. table_a | (parent table)
TABLE_NAME | COLUMN_NAME | COLUMN_KEY |
---|---|---|
table_a | ID | PRIMARY_KEY |
table_a | NAME | - |
table_a | DESCRIPTION | - |
2. table_b (child table)
TABLE_NAME | COLUMN_NAME | COLUMN_KEY |
---|---|---|
table_b | ID | PRIMARY_KEY |
table_b | NAME | - |
table_b | VALUE | - |
table_b | REF_ID | FOREIGN_KEY (from table_a ID) |
Below are the steps that I am following to insert records:
- Begin Transaction; (auto commit is disabled)
- INSERT INTO table_a;
- Preparing required objects
- INSERT INTO table_b;
- COMMIT/ROLLABACK Transaction;
While reaching at step 4 :
"Deadlock found when trying to get lock; try restarting transaction Query being executed when exception was thrown: INSERT INTO table_b (NAME, VALUE, REF_ID) VALUES ("TEST", 'TEST_VALUE', 10) com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction Query being executed when exception was thrown: INSERT INTO table_b (NAME, VALUE, REF_ID) VALUES ("TEST", 'TEST_VALUE', 10)"
Note : I am not doing any other alter operation on the same row.
ISOLATION_LEVEL : REPEATABLE-READ
Please let me know, if any other information are needed. I will update the question as per the request.