spring 事務隔離級別導致的bug

事情是這樣的,ios進貨單有一個數量加一手,減一手的功能,每加減一次就會異步調用後臺的接口,後臺判斷sku如果不存在就插入,存在就更新。
 
問題描述:
當ios發了多次請求後,
在第二次請求的時候,第一次請求插入的sku程序裏查不出來
 
 
但是數據庫裏能查出來
 
 
 
後來仔細研究了下,發現這就是所謂的不可重複讀情況。
 
在applicationContext.xml配置文件中的事務相關模塊把事務隔離級別提成到SERIALIZABLE。
<!-- 進貨單加減一手方法存在不可重複讀情況,提升事務隔離級別 -->
 <tx:method name="changeShoppingCartSkuList" propagation="REQUIRED" isolation="SERIALIZABLE" rollback-for="Exception" />

 

 

問題又來了:
args:
 skuIds[]=122890,122891,122892,122893,122894
 timeid=4152
 orderId=2733
 num=1
 sign=c30fc44b4a7681fbc19bd9c8c2de2237
 authTime=2016-09-10 16:18:17
 authId=900000000000140
exception:
org.springframework.dao.DeadlockLoserDataAccessException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
### The error may exist in com/itonghui/biz/shoppingcart/dao/mapper/OrderListSkuMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from order_list_sku t  where t.sku_id = ?  and order_id = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
; SQL []; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:263)
 
會出現死鎖現象,在方法前面加上synchronized同步解決問題。
 
總結一下,關於異步調用不可重複讀問題解決方案:
1.在applicationContext.xml配置文件中的事務相關模塊把事務隔離級別提成到SERIALIZABLE。
2.在方法前面加上synchronized同步
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章