MySQL:如何找出等待锁定的原因?

时间:2018-12-29 09:00:45

标签: mysql

当我对项目进行压力测试时,有时性能会下降。我在控制台中看到以下提示:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction

然后我在MySQL中查询innodb状态,得到:

=====================================
2018-12-29 15:55:10 0x70000eaf2000 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 29 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 535 srv_active, 0 srv_shutdown, 182220 srv_idle
srv_master_thread log flush and writes: 0
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 461
OS WAIT ARRAY INFO: signal count 425
RW-shared spins 63, rounds 84, OS waits 25
RW-excl spins 109, rounds 2358, OS waits 41
RW-sx spins 9, rounds 232, OS waits 5
Spin rounds per wait: 1.33 RW-shared, 21.63 RW-excl, 25.78 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 11302
Purge done for trx's n:o < 11301 undo n:o < 0 state: running but idle
History list length 29
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281479770005552, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479770002816, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479769999168, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 11300, ACTIVE 42 sec starting index read
mysql tables in use 2, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 10018, OS thread handle 123145553215488, query id 182350 localhost 127.0.0.1 username updating
UPDATE hibernate_sequence
  SET currentValue = currentValue + increment
  WHERE seqname =  NAME_CONST('seq_name',_utf8mb4'hibernate_sequence' COLLATE 'utf8mb4_0900_ai_ci')
Trx read view will not see trx with id >= 11295, sees < 11295
------- TRX HAS BEEN WAITING 42 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2 page no 4 n bits 72 index PRIMARY of table `meeting`.`hibernate_sequence` trx id 11300 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 18; hex 68696265726e6174655f73657175656e6365; asc hibernate_sequence;;
 1: len 6; hex 000000002c20; asc     , ;;
 2: len 7; hex 020000026c0a34; asc     l 4;;
 3: len 4; hex 8153cbec; asc  S  ;;
 4: len 4; hex 80000001; asc     ;;

------------------
---TRANSACTION 11298, ACTIVE 43 sec starting index read
mysql tables in use 2, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 10019, OS thread handle 123145558974464, query id 182348 localhost 127.0.0.1 username updating
UPDATE hibernate_sequence
  SET currentValue = currentValue + increment
  WHERE seqname =  NAME_CONST('seq_name',_utf8mb4'hibernate_sequence' COLLATE 'utf8mb4_0900_ai_ci')
Trx read view will not see trx with id >= 11295, sees < 11295
------- TRX HAS BEEN WAITING 42 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2 page no 4 n bits 72 index PRIMARY of table `meeting`.`hibernate_sequence` trx id 11298 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 18; hex 68696265726e6174655f73657175656e6365; asc hibernate_sequence;;
 1: len 6; hex 000000002c20; asc     , ;;
 2: len 7; hex 020000026c0a34; asc     l 4;;
 3: len 4; hex 8153cbec; asc  S  ;;
 4: len 4; hex 80000001; asc     ;;

------------------
---TRANSACTION 11297, ACTIVE 43 sec starting index read
mysql tables in use 2, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 10022, OS thread handle 123145549578240, query id 182346 localhost 127.0.0.1 username updating
UPDATE hibernate_sequence
  SET currentValue = currentValue + increment
  WHERE seqname =  NAME_CONST('seq_name',_utf8mb4'hibernate_sequence' COLLATE 'utf8mb4_0900_ai_ci')
Trx read view will not see trx with id >= 11295, sees < 11295
------- TRX HAS BEEN WAITING 42 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2 page no 4 n bits 72 index PRIMARY of table `meeting`.`hibernate_sequence` trx id 11297 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 18; hex 68696265726e6174655f73657175656e6365; asc hibernate_sequence;;
 1: len 6; hex 000000002c20; asc     , ;;
 2: len 7; hex 020000026c0a34; asc     l 4;;
 3: len 4; hex 8153cbec; asc  S  ;;
 4: len 4; hex 80000001; asc     ;;

------------------
---TRANSACTION 11296, ACTIVE 43 sec
3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 2
MySQL thread id 10021, OS thread handle 123145548972032, query id 182340 localhost 127.0.0.1 username
Trx read view will not see trx with id >= 11295, sees < 11295
---TRANSACTION 11295, ACTIVE 43 sec starting index read
mysql tables in use 2, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 10016, OS thread handle 123145559580672, query id 182347 localhost 127.0.0.1 username updating
UPDATE hibernate_sequence
  SET currentValue = currentValue + increment
  WHERE seqname =  NAME_CONST('seq_name',_utf8mb4'hibernate_sequence' COLLATE 'utf8mb4_0900_ai_ci')
Trx read view will not see trx with id >= 11295, sees < 11295
------- TRX HAS BEEN WAITING 42 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2 page no 4 n bits 72 index PRIMARY of table `meeting`.`hibernate_sequence` trx id 11295 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 18; hex 68696265726e6174655f73657175656e6365; asc hibernate_sequence;;
 1: len 6; hex 000000002c20; asc     , ;;
 2: len 7; hex 020000026c0a34; asc     l 4;;
 3: len 4; hex 8153cbec; asc  S  ;;
 4: len 4; hex 80000001; asc     ;;

------------------
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
3122 OS file reads, 40273 OS file writes, 26376 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 1.00 writes/s, 0.69 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 3 merges
merged operations:
 insert 3, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 34679, node heap has 2 buffer(s)
Hash table size 34679, node heap has 3 buffer(s)
Hash table size 34679, node heap has 7 buffer(s)
Hash table size 34679, node heap has 1 buffer(s)
Hash table size 34679, node heap has 10 buffer(s)
Hash table size 34679, node heap has 2 buffer(s)
Hash table size 34679, node heap has 2 buffer(s)
Hash table size 34679, node heap has 1 buffer(s)
0.55 hash searches/s, 0.86 non-hash searches/s
---
LOG
---
Log sequence number          369837779
Log buffer assigned up to    369837779
Log buffer completed up to   369837779
Log written up to            369837779
Log flushed up to            369837779
Added dirty pages up to      369837779
Pages flushed up to          369837779
Last checkpoint at           369837779
28743 log i/o's done, 0.31 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 672360
Buffer pool size   8191
Free buffers       4589
Database pages     3574
Old database pages 1302
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 9, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 3059, created 515, written 10416
0.00 reads/s, 0.00 creates/s, 0.52 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 3574, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
5 read views open inside InnoDB
Process ID=97, Main thread ID=0x70000e4d8000 , state=sleeping
Number of rows inserted 9466, updated 3040, deleted 47, read 21095890
0.07 inserts/s, 0.03 updates/s, 0.00 deletes/s, 0.62 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

在以上信息中,我知道某些事务正在等待与表hibernate_sequence相关的锁,该锁用于生成主ID。但是,我找不到有关谁拥有锁的任何信息。另外,我想知道为什么它这么长时间拥有锁。

能否请您提供一些帮助?

0 个答案:

没有答案