com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:400) at com.mysql.jdbc.Util.getInstance(Util.java:383) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
LATEST DETECTED DEADLOCK ------------------------ 2022-07-19 10:10:22 0x7f269177f700 *** (1) TRANSACTION: TRANSACTION 1017957458, ACTIVE 0 sec starting index read mysql tables in use 3, locked 3 LOCK WAIT 4 lock struct(s), heap size 1136, 3 row lock(s) MySQL thread id 13133017, OS thread handle 139806311954176, query id 2776903298 10.**.**.179 k**_base_oniv updating /*id:182a***d*//*ip=10.**.**.179*/update test_table SET is_deleted = 1, mod_time = '2022-07-19 10:10:22.861' WHERE ( user_id = 1548612299617681421 and class_id = 1470379 and is_deleted = 0 ) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 883 page no 6297 n bits 1000 index idx_class_id of table `test_db`.`test_table` trx id 1017957458 lock_mode X waiting *** (2) TRANSACTION: TRANSACTION 1017957457, ACTIVE 0 sec fetching rows mysql tables in use 3, locked 3 83 lock struct(s), heap size 8400, 669 row lock(s) MySQL thread id 13116419, OS thread handle 139803626043136, query id 2776903296 10.**.**.178 k**_base_oniv updating /*id:182a***d*//*ip=10.**.**.178*/update test_table SET is_deleted = 1, mod_time = '2022-07-19 10:10:22.861' WHERE ( user_id = 1548831949420961886 and class_id = 1470379 and is_deleted = 0 ) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 883 page no 6297 n bits 1000 index idx_class_id of table `test_db`.`test_table` trx id 1017957457 lock_mode X *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 883 page no 8596 n bits 152 index PRIMARY of table `test_db`.`test_table` trx id 1017957457 lock_mode X locks rec but not gap waiting *** WE ROLL BACK TRANSACTION (1)
从日志中可以看到死锁发生时间,导致死锁的事务信息,每个事务正在执行的sql,等待锁、持有锁等信息。
下面逐段对日志进行详细分析:
1 2
*** (1) TRANSACTION: TRANSACTION 1017957458, ACTIVE 0 sec starting index read
1017957458 是第一个事务的 id。
ACTIVE 0 sec 表示事务活动时间。
starting index read 表示事务当前状态,这里表示正在读索引;其他可能的事务状态有:fetching rows,updating,deleting,inserting 等。
1 2
mysql tables in use 3, locked 3 LOCK WAIT 4 lock struct(s), heap size 1136, 3 row lock(s)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 883 page no 6297 n bits 1000 index idx_class_id of table `test_db`.`test_table` trx id 1017957458 lock_mode X waiting
WAITING FOR THIS LOCK TO BE GRANTED 表示正在等待锁。
RECORD LOCKS 表示等待的锁类型,这里是记录锁。
space id 883 page no 6297 n bits 1000 表示文件位置,这里不关心。
index idx_class_id of table test_db.test_table 表示锁的索引,以及索引详情。
trx id 1017957458 是事务id。
lock_mode X waiting 表示Next-key 锁,当前处于锁等待状态。
1 2 3 4 5 6 7 8 9 10 11
*** (2) TRANSACTION: TRANSACTION 1017957457, ACTIVE 0 sec fetching rows mysql tables in use 3, locked 3 83 lock struct(s), heap size 8400, 669 row lock(s) MySQL thread id 13116419, OS thread handle 139803626043136, query id 2776903296 10.198.196.178 k**_base_oniv updating /*id:182a1bfd*//*ip=10.198.196.178*/update test_table SET is_deleted = 1, mod_time = '2022-07-19 10:10:22.861' WHERE ( user_id = 1548831949420961886 and class_id = 1470379 and is_deleted = 0 )
事务2这段信息跟事务1类似,不再赘述。
1 2 3 4 5
*** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 883 page no 6297 n bits 1000 index idx_class_id of table `test_db`.`test_table` trx id 1017957457 lock_mode X *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 883 page no 8596 n bits 152 index PRIMARY of table `test_db`.`test_table` trx id 1017957457 lock_mode X locks rec but not gap waiting *** WE ROLL BACK TRANSACTION (1)