解決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:\java\project\bim-service\target\classes\mapper\ApiPropertiesMapper.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,但是這樣耗時比較多

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM