错误调试记录3 并发事物控制及锁得使用

 

 

错误日志如下:

2011-05-04 18:23:12,515 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer#1-2] BatDisburseBoImp.toDisburse(84) | 时间:Wed May 04 18:23:12 CST 2011  内部流水号:WW20110504182312826431   对方订单号:AD283380683  支付失败。

org.springframework.dao.DeadlockLoserDataAccessException: SqlMapClient operation; SQL [];  

--- The error occurred while applying a parameter map. 

--- Check the com.shunwang.swpay.disburse.dao.userAccount.updateCommissionById-InlineParameterMap. 

--- Check the statement (update failed). 

--- Cause: java.sql.SQLException: 事务(进程 ID 64)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  

--- The error occurred while applying a parameter map. 

--- Check the com.shunwang.swpay.disburse.dao.userAccount.updateCommissionById-InlineParameterMap. 

--- Check the statement (update failed). 

--- Cause: java.sql.SQLException: 事务(进程 ID 64)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:  

--- The error occurred while applying a parameter map. 

--- Check the com.shunwang.swpay.disburse.dao.userAccount.updateCommissionById-InlineParameterMap. 

--- Check the statement (update failed). 

--- Cause: java.sql.SQLException: 事务(进程 ID 64)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。

 

解决方法: 把ibatIS文件的账户查询锁 HOLDLOCK  改成 XLOCK 。

 

原因是  : HOLDLOCK   是持有锁 ,在查询里面是共享锁 ,所以会导致死锁。XLOCK 是排他锁。

 

可用以下代码开两个事物提交来模拟死锁:

begin tran
SELECT *
  FROM [swpay].[passport].[user_account] with (HOLDlock) where user_account_id=4
update  [swpay].[passport].[user_account] with (HOLDlock)
set user_account_name = 'test'  where user_account_id=4
commit

原文地址:https://www.cnblogs.com/alaricblog/p/3264671.html