The MySQL transaction is not committed, causing the lock to wait for Lock wait timeout exceeded

java.lang.Exception:
### Error updating database.  Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
; SQL []; Lock wait timeout exceeded; try restarting transaction; nested exception is java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction

After select * from information_schema.innodb_trx, a read-only transaction that has not been committed is found,

After kill reaches the corresponding thread, it is ok.

==============================================

 

Reprint: http://blog.sina.com.cn/s/blog_6bb63c9e0100s7cb.html

MySQL 5.5 -- innodb_lock_wait lock wait

Remember before, when: ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction appeared, it
was a troublesome thing to solve;
especially when a SQL was executed, but not COMMIT, the following SQL wanted to be executed just by being executed. Lock, the timeout is over;
DBA can't start to find out which SQL is locked from the database
; sometimes look at show engine innodb status, combined with show full processlist; can temporarily solve the problem; but it has not been able to accurately locate;

In 5.5, three tables about locks (MEMORY engine) have been added to the information_schema library;
innodb_trx ## All transactions currently running
innodb_locks ## Currently occurring locks
innodb_lock_waits ## Correspondence of lock waiting

I'm very excited to see this; this solves a big problem, let's take a look at the table structure first

 


root @127.0.0.1   : information_schema 13:28:38> desc innodb_locks;
+-------------+------------------- --+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-- -----------+---------------------+------+-----+--- ------+-------+
| lock_id | varchar(81) | NO | | | |#Lock ID
| lock_trx_id | varchar(18) | NO | | | |#The transaction ID that owns the lock
| lock_mode | varchar(32) | NO | | | |#Lock mode
| lock_type | varchar(32) | NO | | | |#Lock type
| lock_table | varchar(1024) | NO | | | |#Locked table
| lock_index | varchar(1024) | YES | | NULL | |#Locked index
| lock_space | bigint(21) unsigned | YES | | NULL | |#Locked tablespace number
| lock_page | bigint(21) unsigned | YES | | NULL | |#Number of locked pages
| lock_rec | bigint(21) unsigned | YES | | NULL | |#Locked record number
| lock_data | varchar(8192) | YES | | NULL | |#Locked data
+--------- ----+---------------------+------+-----+---------+ -------+
10 rows in set (0.00 sec)

root @127.0.0.1   : information_schema 13:28:56> desc innodb_lock_waits;
+----------------- --+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+---- --+-----+---------+-------+
| requesting_trx_id | varchar(18) | NO | | | |#Transaction ID of requesting lock
| requested_lock_id | varchar (81) | NO | | | |# The lock ID of the requested lock
| blocking_trx_id | varchar(18) | NO | | | | # The current transaction ID of the lock
| blocking_lock_id | varchar(81) | NO | | | The lock ID that owns the lock
+-------------------+-------------+------+-----+-- -------+-------+
4 rows in set (0.00 sec)

root @127.0.0.1   : information_schema 13:29:05> desc innodb_trx ;
+--------- -------------------+---------------------------------+------+- ----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+- ---------------------------------------+---------------------------------+ ------+-----+--------------------+-------+
| trx_id | varchar(18) | NO | | | |#Transaction ID
| trx_state | varchar(13) | NO | | | |# Transaction state:
| trx_started | datetime | NO | | 0000-00-00 00:00:00 | | # Transaction start time ;
| trx_requested_lock_id | varchar(81) | YES | | NULL | |#innodb_locks.lock_id
| trx_wait_started | datetime | YES | | NULL |
| trx_weight | bigint(21) unsigned | NO | | 0 | |#
| trx_mysql_thread_id | bigint(21) unsigned | NO | | 0 | |# Transaction thread ID
| trx_query | varchar(1024) | YES | | NULL | |# Specific SQL statement
| trx_operation_state | varchar(64) | YES | | NULL | |#The current operation state of the transaction
| trx_tables_in_use | bigint(21) unsigned | NO | | 0 | |# How many tables are used in the transaction
| trx_tables_locked | bigint (21) unsigned | NO | | 0 | |# How many locks the transaction owns
| trx_lock_structs | bigint(21) unsigned | NO | | 0 | |#
| trx_lock_memory_bytes | bigint(21) unsigned | NO | | 0 | |# Transaction Locked memory size (B)
| trx_rows_locked | bigint(21) unsigned | NO | | 0 | |#Number of rows locked by transaction
| trx_rows_modified | bigint(21) unsigned | NO | | 0 | |#Rows changed by transaction number
| trx_concurrency_tickets | bigint(21) unsigned | NO | | 0 | | # Transaction and invoice number
| trx_isolation_level | varchar(16) | NO | | | |#Transaction isolation level
| trx_unique_checks | int(1) | NO | | 0 | |# Uniqueness check
| trx_foreign_key_checks | int(1) | NO | | # Whether foreign key check
| trx_last_foreign_key_error | varchar(256) | YES | | NULL | | # Last foreign key error
| trx_adaptive_hash_latched | int(1) | NO | | 0 | |#
| trx_adaptive_hash_timeout | bigint(21) unsigned | NO | | 0 | |#
+----------------------------+-------------- -------+------+-----+---------------------+------- +
22 rows in set (0.01 sec)

Let's take a look at the data:
##Create test data:
use test;
create table tx1
(id int primary key ,
c1 varchar(20),
c2 varchar(30))
engine=innodb default charset = utf8 ;

insert into tx1 values
(1,'aaaa','aaaaa2'),
(2,'bbbb','bbbbb2'),
(3,'cccc','ccccc2');

commit;

###产生事务;
### Session1
start transaction;
update tx1 set c1='heyf',c2='heyf' where id =3 ;

## 产生事务,在innodb_trx就有数据 ;
root@127.0.0.1  : information_schema 13:38:21> select * from innodb_trx G
*************************** 1. row ***************************
trx_id: 3669D82
trx_state: RUNNING
trx_started: 2010-12-24 13:38:06
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 3
trx_mysql_thread_id: 2344
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 1
trx_rows_modified: 1
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
1 row in set (0.00 sec)

### 由于没有产生锁等待,下面两个表没有数据 ;
root@127.0.0.1  : information_schema 13:38:31> select * from innodb_lock_waits G
Empty set (0.00 sec)

root@127.0.0.1  : information_schema 13:38:57> select * from innodb_locks G
Empty set (0.00 sec)

#### 产生锁等待
#### session 2
start transaction;
update tx1 set c1='heyfffff',c2='heyffffff' where id =3 ;


root@127.0.0.1  : information_schema 13:39:01> select * from innodb_trx G
*************************** 1. row ***************************
trx_id: 3669D83 ##第2个事务
trx_state: LOCK WAIT ## 处于等待状态
trx_started: 2010-12-24 13:40:07
trx_requested_lock_id: 3669D83:49:3:4 ##请求的锁ID
trx_wait_started: 2010-12-24 13:40:07
trx_weight: 2
trx_mysql_thread_id: 2346 ##线程 ID
trx_query: update tx1 set c1='heyfffff',c2='heyffffff' where id =3
trx_operation_state: starting index read
trx_tables_in_use: 1 ##需要用到1个表
trx_tables_locked: 1 ##有1个表被锁
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 1
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
*************************** 2. row ***************************
trx_id: 3669D82 ##第1个事务
trx_state: RUNNING
trx_started: 2010-12-24 13:38:06
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 3
trx_mysql_thread_id: 2344
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 1
trx_rows_modified: 1
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
2 rows in set (0.00 sec)

root@127.0.0.1  : information_schema 13:40:12> select * from innodb_locks G
*************************** 1. row ***************************
lock_id: 3669D83:49:3:4 ## 第2个事务需要的锁
lock_trx_id: 3669D83
lock_mode: X
lock_type: RECORD
lock_table: `test`.`tx1`
lock_index: `PRIMARY`
lock_space: 49
lock_page: 3
lock_rec: 4
lock_data: 3
*************************** 2. row ***************************
lock_id: 3669D82:49:3:4 ## 第1个事务需要的锁
lock_trx_id: 3669D82
lock_mode: X
lock_type: RECORD
lock_table: `test`.`tx1`
lock_index: `PRIMARY`
lock_space: 49
lock_page: 3
lock_rec: 4
lock_data: 3
2 rows in set (0.00 sec)

root @127.0.0.1   : information_schema 13:40:15> select * from innodb_lock_waits G
************************** 1. row ** ************************
requesting_trx_id: 3669D83 ## The transaction requesting the lock
requested_lock_id: 3669D83:49:3:4 ## The lock ID of the requesting lock
blocking_trx_id : 3669D82 ## The transaction that owns the lock
blocking_lock_id:  3669D82:49:3:4 ## The lock ID that owns the lock
1 row in set (0.00 s ec)

 

http://blog.csdn.net/mangmang2012/article/details/9207007

http://blog.itpub.net/12679300/viewspace-1420031/

http://my.oschina.net/quanzhong/blog/222091

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326859441&siteId=291194637