前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Lock wait timeout exceeded; try restarting transaction

Lock wait timeout exceeded; try restarting transaction

作者头像
IT云清
发布2019-01-22 15:10:47
3.7K0
发布2019-01-22 15:10:47
举报
文章被收录于专栏:IT云清IT云清

这个错误,应该是锁等待超时,事务长时间没有提交,导致了回滚。

1.错误如下:

代码语言:javascript
复制
org.springframework.dao.CannotAcquireLockException: 
### Error updating database.  Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO industrial_output         VALUES (null,?,?,         ?,?,?,         ?,?,?,         ?,?,NOW(),NULL)
### Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
; SQL []; Lock wait timeout exceeded; try restarting transaction; nested exception is java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:259)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
    at com.sun.proxy.$Proxy19.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:240)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:46)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
    at com.sun.proxy.$Proxy95.add(Unknown Source)

2.发生场景:

代码语言:javascript
复制
    //类上有@Transactional注解
    @Override
    public void upload(String platFrom,String yearAndMonth, File file) throws Exception{
        //删除历史数据
        industrialOutputDao.delete(platFrom,yearAndMonth);
        fileUploadHistoryDao.delete(platFrom,yearAndMonth+"-01",FILE_TYPE);
        logger.info("==========>删除历史数据");

        List<IndustrialOutput> industrialOutputList = this.parseExcel(file, platFrom, yearAndMonth);

        if(CollectionUtils.isNotEmpty(industrialOutputList)){
            industrialOutputList.parallelStream().forEach(industrialOutput -> {
                industrialOutputDao.add(industrialOutput);//出错位置!!!
            });
        }
        logger.info("===========>工业产值表格解析完毕,数据写入完毕");
    }

3.解决方案:

方法级别添加事务,并指定事务的传播级别:

代码语言:javascript
复制
@Transactional(propagation= Propagation.SUPPORTS)

1.查询等待锁的进程

代码语言:javascript
复制
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
这里写图片描述
这里写图片描述

2.查询所有被锁的

代码语言:javascript
复制
select * from information_schema.innodb_trx;
这里写图片描述
这里写图片描述

事务传播机制可参考:https://blog.csdn.net/yuanlaishini2010/article/details/45792069

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年09月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.错误如下:
  • 2.发生场景:
  • 3.解决方案:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档