MySQL事务锁问题-Lock wait timeout exceeded; try restarting transaction

测之前的功能突然报了一个没见过的异常,有点鸡冻٩(๑>◡<๑)۶

org.springframework.dao.CannotAcquireLockException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
### The error may involve com.focous.mapper.smart.SsdCarAlarmMapper.updateSsdCarAlarmBySmallPicName-Inline
### The error occurred while setting parameters
### SQL: update alarminfo_ssd_car set  deviceName=?, deviceid=?, channelid=?, deviceIp=?, type=?, alarmtime=?, x=?, y=?, h=?, w=?, escortType=?, carColor=?, carType=?, detail=? where alarmseq=? and picNameS=?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
; SQL []; Lock wait timeout exceeded; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction

大致意思是讲:锁等待超时,是当前事务在等待其它事务释放锁资源造成的。

问题现象:

更新报警数据异常,数据库信息不完整且记录数不对应,后台报如上所示问题。

问题场景:

1、在同一事务内先后对同一条数据进行插入和更新操作(举手);

2、多台服务器操作同一数据库;

3、瞬时出现高并发现象;

问题原因:

1、mysql数据库采用InnoDB模式,默认参数:innodb_lock_wait_timeout设置锁等待的时间是50s,一旦数据库锁超过这个时间就会报错;

2、在高并发的情况下,spring事务造成数据库死锁,后续操作超时抛出异常。

解决方法:

1、查询是否锁表
show open tables where in_use>0;
 
2、查询进程
show processlist
查询到相对应的进程,然后 kill id

3、当前运行的所有事务
SELECT * FROM information_schema.INNODB_TRX;
 
4、查看正在锁的事务
select * from information_schema.innodb_locks; 
 
5、查看等待锁的事务
select * from information_schema.innodb_lock_waits;

6、批量删除事务表中的事务
通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令。
select concat('KILL ',id,';') from information_schema.processlist where user='cms_bokong';

查看事务表INNODB_TRX,里面是否有正在锁定的事务线程,看看ID是否在show processlist里面的sleep线程中,如果是就证明这个sleep的线程事务一直没有commit或者rollback,而是卡住了,需要手动kill掉。

kill掉以后再执行SELECT * FROM information_schema.INNODB_TRX; 就是空了。这时候系统就正常了

故障排除

mysql都是autocommit配置:

如果是0 ,则改为1

 set global autocommit=1;

mysql的引擎检查(mysql5.5.5以前默认是MyISAM,mysql5.5.5以后默认是InnoDB):

show ENGINES

发布了91 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/hjing123/article/details/98058426