解决Lock wait timeout exceeded; try restarting transaction问题
1 问题
mysql数据库修改某条记录的时候出现报错:
org.springframework.dao.CannotAcquireLockException:
Error updating database. Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
The error may involve com.*.dao.mapper.PhoneFlowMapper.updateByPrimaryKeySelective-Inline
The error occurred while setting parameters
2 原因
在高并发的情况下,Spring事物造成mysql数据库事务未提交产生死锁,后续操作超时抛出异常。
mysql数据库采用InnoDB模式,默认参数:innodb_lock_wait_timeout设置锁等待的时间是50s,一旦数据库锁超过这个时间就会报错。
3 解决方法
通过以下语句查询出未提交的事务,然后kill掉事务
select * from information_schema.innodb_trx
kill 26060
首先执行:
show full processlist; //列出当前的操作process,一般会看到很多waiting的process,说明已经有卡住的proces了,我们要杀死这些process!!
再执行:
kill processid; //processid表示process的id,比如kill 3301,就会将id为3301的process杀死。
使用 kill 将所有的 id 杀死。然后重启MySQL,一般就会解决了。如果还不行,那应该是不可能的吧。。。