Reputation: 4692
I am using mysql database I tried to find the reason for deadlock but I am unable to find it so pasting my code here.
Can anyone please tell me how/when this deadlock is coming
.
Below code is executed by 5 threads simultaneously
but totally on different records (first thread is executing for first 100 of F_Table
then another thread will execute for 101 to 200 records of F_Table and so on).
String deleteFQuery = "DELETE FROM F_Table WHERE ID IN (?,?,? ...25 times )"; // Deleting old record for given id
String insertFQuery = "INSERT INTO F_Table (columns..) values (?,?..)"; // Reinserting for same id same with updated values
String deleteMpQuery = "DELETE FROM MP_Table WHERE ID IN (?,?,? ...25 times)";
String updateMQuery = "UPDATE M_Table SET COLUMN1=? WHERE ID IN (?,?,? ...25 times)";
pstmt1 = con.prepareStatement(insertFQuery);
for (i -> 100 times) {
pstmt1.setString(1, value1); //Values will be set from a list which is getting iterated in this for loop.
pstmt1.setString(2, value2);
pstmt1.setString(3, value3);
pstmt1.setString(4, value4);
pstmt1.addBatch();
if ((i1) % 25 == 0) {
pstmt2 = con.prepareStatement(deleteFuzzyQuery);
pstmt2.setInt() //this will set 25 values in for loop
pstmt2.executeUpdate();
pstmt2.close();
synchronized (this) {
pstmt1.executeBatch(); // Deadlock found when trying to get lock; try restarting transaction
// Exception : class java.sql.BatchUpdateException
pstmt1.close();
}
pstmt2 = con.prepareStatement(deleteMpQuery);
pstmt2.setInt() //this will set 25 values in for loop
pstmt2.executeUpdate();
pstmt2.close();
pstmt2 = con.prepareStatement(updateMQuery);
pstmt2.setInt() //this will set 25 values in for loop
pstmt2.executeUpdate();
pstmt2.close();
con.commit();
pstmt1 = con.prepareStatement(insertFQuery);
}
}
Note: Deadlock is coming once in 10 time or so.
Thanks.
Update:
After executing SHOW ENGINE INNODB STATUS
I found below log content. I am really new to read this. Can anyone please help me to find what kind of lock it is having.
LATEST DETECTED DEADLOCK
------------------------
2014-11-13 13:13:42 1070
*** (1) TRANSACTION:
TRANSACTION 8871318, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 312, 2 row lock(s)
MySQL thread id 7761, OS thread handle 0x1360, query id 145308 dbuser update
INSERT INTO F_TABLE(ID, ...) VALUES ('1',...)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 28831 page no 3 n bits 72 index `PRIMARY` of table `dbname`
.`f_table` trx id 8871318 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** (2) TRANSACTION:
TRANSACTION 8871319, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
3 lock struct(s), heap size 312, 2 row lock(s)
MySQL thread id 7763, OS thread handle 0x1070, query id 145309 dbuser update
INSERT INTO F_TABLE(ID, ...) VALUES ('101',...)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 28831 page no 3 n bits 72 index `PRIMARY` of table `dbname`
.`f_table` trx id 8871319 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 28831 page no 3 n bits 72 index `PRIMARY` of table `dbname`
.`f_table` trx id 8871319 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** WE ROLL BACK TRANSACTION (2)
Update 2:
Added synchronized block
. Will that help to avoid deadlock while executing pstmt1.executeBatch();
?
Upvotes: 0
Views: 262
Reputation: 695
run SHOW ENGINE INNODB STATUS
(if your db engine is innodb) in mysql.You will get detailed
engine status with last detected deadlock details.There you will see which query goes in deadlock.Now to able to answer your question result from show engine query will be helpful.
Upvotes: 1