如果有一些特定的SqlException
(例如连接丢失),我必须修改用于处理我的SQL调用的静态类,以便重试请求。在SqlException上自动重试请求
这里是我的方法来调用存储过程:
public static int CallExecuteNonQuery(string storeProcName, Action<SqlCommand> fillParamsAction, Action afterExecution, BDDSource source)
{
int result;
try
{
using (var connection = InitSqlConnection(source))
using (var command = new SqlCommand(storeProcName, connection))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
command.CommandType = CommandType.StoredProcedure;
if (fillParamsAction != null)
fillParamsAction(command);
result = command.ExecuteNonQuery();
if (afterExecution != null)
afterExecution();
}
}
catch (SqlException sqlExn)
{
Logger.Exception(string.Format("SQL CRITICAL ERROR. Stored Proc Name : {0}", storeProcName), sqlExn);
throw;
}
catch (Exception exception)
{
Logger.Exception(string.Format("SOFTWARE CRITICAL ERROR. Stored Proc Name : {0}", storeProcName), exception);
throw;
}
return result;
}
继this link,我试图重试请求尽可能多的时间,因为它是可配置的。
我得到了下面的代码:
public static int CallExecuteNonQuery(string storeProcName, Action<SqlCommand> fillParamsAction, Action afterExecution, BDDSource source)
{
bool RetryRequest = true;
int result = 0;
for (int i = 0; i < Properties.Settings.Default.Request_MaximumRetry; i++)
{
try
{
if (RetryRequest)
{
using (var connection = InitSqlConnection(source))
using (var command = new SqlCommand(storeProcName, connection))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
command.CommandType = CommandType.StoredProcedure;
if (fillParamsAction != null)
fillParamsAction(command);
result = command.ExecuteNonQuery();
if (afterExecution != null)
afterExecution();
}
RetryRequest = false;
}
}
catch (SqlException sqlExn)
{
if (sqlExn.Errors.Cast<SqlError>().All(x => (x.Class >= 16 && x.Class < 22) || x.Class == 24))
{
RetryRequest = true;
continue;
}
Logger.Exception(string.Format("SQL CRITICAL ERROR. Stored Proc Name : {0}", storeProcName), sqlExn);
RetryRequest = false;
throw;
}
catch (Exception exception)
{
Logger.Exception(string.Format("SOFTWARE CRITICAL ERROR. Stored Proc Name : {0}", storeProcName), exception);
RetryRequest = false;
throw;
}
}
return result;
}
但我的修改是不完美的。例如,在3次异常重试之后,在离开boucle之前,代码不会抛出并进入continue;
部分。
这是'when'的巧妙使用,虽然我不确定是否有副作用是安全的。 – Douglas