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,但是这样耗时比较多

  • 相关阅读:
    [算法]最长子数组问题
    [算法]K-SUM problem
    selenium-远程调用
    pytest-fixture
    Ubuntu18.04中安装virtualenv和virtualenvwrapper
    shell
    mac保存远程链接
    css
    js基础
    css基础
  • 原文地址:https://www.cnblogs.com/baby123/p/11365641.html
Copyright © 2011-2022 走看看