zoukankan      html  css  js  c++  java
  • 解决org.springframework.dao.DeadlockLoserDataAccessException

    添加链接池后批量添加更新出现了死锁

    org.springframework.dao.DeadlockLoserDataAccessException:
    ### Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    ### The error may exist in file [E:javaprojectim-service argetclassesmapperApiPropertiesMapper.xml]
    ### The error may involve com.tydt.bim.dao.ApiPropertiesMapper.batchAddProperties-Inline
    ### The error occurred while setting parameters
    ### SQL: insert into api_properties (`guid`,`name`,`file_id`,`value`,`unit`) values (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) ON DUPLICATE KEY UPDATE `value`=values(`value`),`unit`=values(`unit`)
    ### Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    ; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction

    INSERT ON DUPLICATE KEY在执行时,innodb引擎会先判断插入的行是否产生重复key错误

    如果存在,在对该现有的行加上S(共享锁)锁,返回该行数据给mysql,然后mysql执行完duplicate后的update操作,然后对该记录加上X(排他锁),最后进行update写入

    如果有两个事务并发的执行同样的语句,那么就会产生death lock

    解决方法:

    1、尽量不对存在多个组合唯一键的table上使用该语句

    2、在有可能有并发事务执行的insert 的内容一样情况下不使用该语句

      先用select查看是否存在,再决定是insert还是update,但是这样耗时比较多

  • 相关阅读:
    国内的pythoner强烈建议使用豆瓣的pypi源 zz
    python3.x中的urllib模块
    idcheck.py
    17、关于hibernate的N+1问题
    16、【转】Hibernate 原汁原味的四种抓取策略
    1、SSH的整合---->将Struts2整合到Spring中
    1、利用json2html.js处理json数据
    1、Ubuntu14.04使用root登陆帐户
    2、CentOS中修改yum源
    1、在CentOS上安装Java JDK的步骤
  • 原文地址:https://www.cnblogs.com/baby123/p/11365641.html
Copyright © 2011-2022 走看看