我们正在处理死锁问题,并找到了解决死锁异常并重新提交6次的解决方案。用户很高兴他们没有得到任何错误,但是在2小时后抛出一个新的错误,生成时出现以下错误" Usp_updateXXXX抛出了太多的争论" 。只要应用遇到deaadlock异常,就会发生此问题。
请在下面找到代码段 { // msg = string.Empty;
SqlConnection _con = new SqlConnection(Conn);
SqlCommand _cmd = new SqlCommand();
SqlDataAdapter _adp = new SqlDataAdapter(_cmd);
_cmd.Connection = _con;
bool isDeadlock = false;
int reTry = 0;
//Check for Deadlock and retry for 6 times to send the same request
do
{
msg = string.Empty;
try
{
_con.Open();
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.CommandText = "usp_UpdateXXXX";
//Assign the command parametrs
_cmd.ExecuteNonQuery();
isDeadlock = false;
}
catch (Exception ex)
{
if ((ex.Message.Contains("was deadlocked on lock resources with another process and has been chosen as the deadlock victim")) || (ex.Message.Contains("deadlock")) || (ex.Message.Contains("Transaction (Process ID")))
{
isDeadlock = true;
Thread.Sleep(5000);
reTry++;
}
else
{
msg = ex.Message;
isDeadlock = false;
}
}
finally
{
con.Close();
}
} while (reTry < 6 && isDeadlock == true);
任何人都可以帮助我导致抛出异常的代码错误在哪里。
此致 PRASHANT
答案 0 :(得分:1)
问题发生的原因是使用了循环。实际上,当异常发生时,它再次调用循环而不清除现有参数,因此添加新参数。
要解决此问题,我在异常部分清除了参数。
try{
//statements here
}
catch (Exception ex)
{
if ((ex.Message.Contains("was deadlocked on lock resources with another process and has been chosen as the deadlock victim")) || (ex.Message.Contains("deadlock")) || (ex.Message.Contains("Transaction (Process ID")))
{
//used below statement
command.Parameters.Clear() ;
isDeadlock = true;
Thread.Sleep(5000);
reTry++;
}
else
{
msg = ex.Message;
isDeadlock = false;
}
}
这解决了我的问题。
此致 PRASHANT