美文网首页
mysql报错 Lock wait timeout exceed

mysql报错 Lock wait timeout exceed

作者: 饱饱抓住了灵感 | 来源:发表于2024-12-10 08:44 被阅读0次

一、报错信息

Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting tran
saction
; Lock wait timeout exceeded; try restarting transaction; nested exception is com.mysql.cj.jdbc.exceptions.MySQLTransactio
nRollbackException: Lock wait timeout exceeded; try restarting transaction
        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTransla
tor.java:267)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionT
ranslator.java:72)
        at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
        at com.sun.proxy.$Proxy183.update(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:287)
        at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:65)
        at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
        at com.sun.proxy.$Proxy184.updateById(Unknown Source)
        at com.baomidou.mybatisplus.extension.service.IService.updateById(IService.java:144)
        at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
...

二、问题原因

A事务对记录1进行更新/删除操作的请求未commit时,B事务对记录1上的表进行更新/删除操作。此时,B会等A提交事务,释放表锁。当等待时间超过innodb_lock_wait_timeout时(默认50秒),会产生“LOCK WAIT”事务。

innodb_lock_wait_timeout 是一个系统变量,用于设置InnoDB存储引擎在回滚由于锁等待而等待的事务之前等待单个锁的时间(以秒为单位)。

要查询当前的 innodb_lock_wait_timeout 设置,可以使用以下SQL命令:

SHOW VARIABLES LIKE 'innodb_lock_wait_timeout';

mysql更多全局变量查询可以参考MySQL show语句大全

如果想要设置这个值,可以在MySQL配置文件(通常是my.cnf或my.ini)中动态设置它,然后重启MySQL服务,或者在运行时使用以下命令进行设置:

SET GLOBAL innodb_lock_wait_timeout = 60;

三、问题解决

出现场景:

  • 在事务方法中出现了死循环, 循环条件一直没能结束, 于是无法提交, 导致另一条请求执行更新或删除操作时报错了, 注意跟踪日志, 解决死循环即可
  • 在微服务体系中, 事务方法里发生了错误的循环调用, 如模块A的的事务方法中处理数据1, 去调用了模块B的方法, 而模块B的方法又调用了模块A中某一个处理数据1的方法

相关文章

网友评论

      本文标题:mysql报错 Lock wait timeout exceed

      本文链接:https://www.haomeiwen.com/subject/rtxasjtx.html