1. 程式人生 > >org.springframework.dao.CannotAcquireLockException異常分析

org.springframework.dao.CannotAcquireLockException異常分析

錯誤資訊如下:

  1. 2017-09-27 16:27:16.153 - 【com.ldyun.base.service.impl.BaseRetailOrderServiceImpl】 - 新增零售商品訂單~org.springframework.dao.CannotAcquireLockException:
  2. ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try
    restarting transaction
  3. ### The error may involve com.ldyun.retail.mapper.RetailGoodsMapper.updateBySql-Inline
  4. ### The error occurred while setting parameters
  5. ### SQL: update retail_goods SET stocks = stocks - CASE id WHEN 83 THEN 1 END,saleCount = saleCount + CASE
    id WHEN 83 THEN 1 END WHERE id IN (83)
  6. ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
  7. ; SQL []; Lock wait timeout exceeded; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try
    restarting transaction~
經過網上資料查詢,原因為:Spring 事務巢狀造成死鎖。

該異常為一個service中呼叫了另一個service,兩個service對同一表進行操作,造成事務巢狀,從而死鎖。

解決辦法:在當前方法前加入@Transactional(propagation=Propagation.SUPPORTS)