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

  • 相关阅读:
    20200924-2 功能测试
    作业要求20200924-4 代码规范,结对要求
    20200924-1 每周例行报告
    20200929-git地址
    白名单
    作业要求 20200917-1 每周例行报告
    词频统计 SPEC
    20200910-1 每周例行报告
    20200924-3 单元测试,结对
    20200924-5 四则运算试题生成,结对
  • 原文地址:https://www.cnblogs.com/baby123/p/11365641.html
Copyright © 2011-2022 走看看