添加連接池後批量添加更新出現了死鎖java
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 transactionmysql
INSERT ON DUPLICATE KEY在執行時,innodb引擎會先判斷插入的行是否產生重複key錯誤spring
若是存在,在對該現有的行加上S(共享鎖)鎖,返回該行數據給mysql,而後mysql執行完duplicate後的update操做,而後對該記錄加上X(排他鎖),最後進行update寫入sql
若是有兩個事務併發的執行一樣的語句,那麼就會產生death lockapi
解決方法:併發
一、儘可能不對存在多個組合惟一鍵的table上使用該語句app
二、在有可能有併發事務執行的insert 的內容同樣狀況下不使用該語句ui
先用select查看是否存在,再決定是insert仍是update,可是這樣耗時比較多rest