錯誤日志:
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 cn.changemax.dao.FilmInfoDAO.batchInsert-Inline
### The error occurred while setting parameters
### 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() )
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
; ]; Lock wait timeout exceeded; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:262)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy101.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy122.batchInsert(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy123.batchInsert(Unknown Source)
at cn.changemax.service.impl.FilmInfoServiceImpl.selectivityInsertFilmInfoList(FilmInfoServiceImpl.java:232)
at cn.changemax.task.SupplementaryFilmDataTask.threadPoolUpdateFilm(SupplementaryFilmDataTask.java:106)
at cn.changemax.task.SupplementaryFilmDataTask.configureTasks(SupplementaryFilmDataTask.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:952)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3051)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
at com.sun.proxy.$Proxy176.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy174.update(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 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;