事情是這樣的,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同步
