MySQL锁等待超时问题记录 - smile0821/learngit GitHub Wiki

生产环境代码提示回滚异常,锁等待超时
错误信息如下:

### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: REPLACE INTO   wp_mo_t_4 (?,?)..... 

场景:该错误信息只在操作大数据量时出现,用户请求上传操作,中间等待时间过长,客户重新取消了请求,重新对比差异后再次操作上传,中间取消、上传、取消、上传操作重复三次以上,等待时间达20min以上,后台跟踪日志提示锁等待超时。 分析:用户第一次请求操作之后,数据库还在执行更新操作,再次请求,该事务等待,重复请求的这些事务都在等待,最终导致等待超时 模拟场景:

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就有数据 ;
select * from information_schema.innodb_trx;
### 由于没有产生锁等待,下面两个表没有数据 ;
select * from information_schema.innodb_lock_waits;
select * from information_schema.innodb_locks;



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


select * from information_schema.innodb_lock_waits;
requesting_trx_id: 3669D83 ## 请求锁的事务
requested_lock_id: 3669D83:49:3:4 ## 请求锁的锁ID
blocking_trx_id:3669D82 ## 拥有锁的事务
blocking_lock_id: 3669D82:49:3:4## 拥有锁的锁ID

ctrl+f6 show processlist; SELECT * FROM information_schema.innodb_trx;

摘自:https://blog.csdn.net/yu757371316/article/details/53524685