当前位置:   article > 正文

解决异常【MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction】_cause: com.mysql.jdbc.exceptions.jdbc4.mysqltransa

cause: com.mysql.jdbc.exceptions.jdbc4.mysqltransactionrollbackexception: lo

错误日志:

  1. org.springframework.dao.CannotAcquireLockException:
  2. ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
  3. ### The error may involve cn.changemax.dao.FilmInfoDAO.batchInsert-Inline
  4. ### The error occurred while setting parameters
  5. ### SQL: insert into t_film_info ( film_id, film_link, film_name, film_image_link, pic_id, film_star, film_intro, film_director, film_type, film_area, film_years, film_play_number, create_time, modify_time) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() ) , ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now() )
  6. ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
  7. ; ]; Lock wait timeout exceeded; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
  8. at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:262)
  9. at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
  10. at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
  11. at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
  12. at com.sun.proxy.$Proxy101.insert(Unknown Source)
  13. at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
  14. at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
  15. at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
  16. at com.sun.proxy.$Proxy122.batchInsert(Unknown Source)
  17. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  18. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  19. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  20. at java.lang.reflect.Method.invoke(Method.java:498)
  21. at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
  22. at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
  23. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
  24. at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  25. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
  26. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
  27. at com.sun.proxy.$Proxy123.batchInsert(Unknown Source)
  28. at cn.changemax.service.impl.FilmInfoServiceImpl.selectivityInsertFilmInfoList(FilmInfoServiceImpl.java:232)
  29. at cn.changemax.task.SupplementaryFilmDataTask.threadPoolUpdateFilm(SupplementaryFilmDataTask.java:106)
  30. at cn.changemax.task.SupplementaryFilmDataTask.configureTasks(SupplementaryFilmDataTask.java:64)
  31. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  32. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  33. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  34. at java.lang.reflect.Method.invoke(Method.java:498)
  35. at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
  36. at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
  37. at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
  38. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  39. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  40. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
  41. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
  42. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  43. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  44. at java.lang.Thread.run(Thread.java:748)
  45. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
  46. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  47. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  48. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  49. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  50. at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
  51. at com.mysql.jdbc.Util.getInstance(Util.java:408)
  52. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:952)
  53. at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
  54. at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
  55. at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
  56. at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
  57. at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
  58. at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
  59. at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
  60. at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3051)
  61. at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
  62. at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049)
  63. at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
  64. at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
  65. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  66. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  67. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  68. at java.lang.reflect.Method.invoke(Method.java:498)
  69. at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
  70. at com.sun.proxy.$Proxy176.execute(Unknown Source)
  71. at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
  72. at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
  73. at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
  74. at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
  75. at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
  76. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  77. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  78. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  79. at java.lang.reflect.Method.invoke(Method.java:498)
  80. at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
  81. at com.sun.proxy.$Proxy174.update(Unknown Source)
  82. at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
  83. at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
  84. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  85. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  86. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  87. at java.lang.reflect.Method.invoke(Method.java:498)
  88. at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
  89. ... 33 common frames omitted

 

错误产生的原因就是:我有一条sql在数据库执行,很长时间,刚好的delete操作,然后这边项目中刚好执行一条批量新增的sql,两者超时冲突了。

所以说这两者就涉及到事务锁的问题的了,接口响应时间超长,耗时几十秒才返回错误提示,后台日志中出现Lock wait timeout exceeded; try restarting transaction的错误,出现了高并发现象。

那么我们来说说如何解决方案:

当务之急,也是要看看数据库中有没有比较长时间执行的sql:

show  processlist;

当前所运行的所有事务

SELECT * FROM information_schema.INNODB_TRX;

当前所有的锁

SELECT * FROM information_schema.INNODB_LOCKs;

锁等待的对应的关系

SELECT * FROM information_schema.INNODB_LOCK_waits;

 

 

那么我们看到事务表中INNODB_TRX,里面是否有正在锁定的事务线程,看看ID是否在show processlist里面的sleep线程中,如果有,那么就证明了这个休眠的线程事务一直没有commit(提交)或者roolback(回滚)而是卡住了,所以,我们需要人为介入,kill掉。

如果发现了好多事务任务,那最好都kill掉。

命令为

select concat('KILL ',id,';') from information_schema.processlist where user='cms_bokong';

通过information_schema.processlist表中的连接信息生成需要处理掉的Mysql连接的语句临时文件,然后执行文件中生成的指令。然后我们获取到了对应任务的id,一个一个 kill id就行了。

然后我们再去找还在进行事务的任务,就会发现空掉了。

 

应急处理完成后,我们就需要核对原因,防止以后再出现:

1.mysql的引擎检查,可以检查一下数据库引擎是不是InnoDB(mysql5.5.5以前默认是MyISAM,Mysql5.5.5以后默认是InnoDB),show engines;#检查命令

如果不是INNDB,那么就改为InnDB;

命令为:

查看表使用的存储引擎

show table status from db_name where name='table_name';

修改表的存储引擎

alter table table_name engine=innodb;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/729308
推荐阅读
相关标签
  

闽ICP备14008679号