I have to modify my static class used to process my SQL calls in order to retry the request if there is some specific SqlException
(connection lost for example).
Here is my method used to call a stored procedure:
public static int CallExecuteNonQuery(string storedProcName, Action<SqlCommand> fillParamsAction, Action afterExecution, BDDSource source)
{
int result;
try
{
using (var connection = InitSqlConnection(source))
using (var command = new SqlCommand(storedProcName, 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}", storedProcName), sqlExn);
throw;
}
catch (Exception exception)
{
Logger.Exception(string.Format("SOFTWARE CRITICAL ERROR. Stored Proc Name : {0}", storedProcName), exception);
throw;
}
return result;
}
Following this link, I'm trying to retry the request as many time as it's configured for.
I got the following code:
public static int CallExecuteNonQuery(string storedProcName, 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(storedProcName, 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}", storedProcName), sqlExn);
RetryRequest = false;
throw;
}
catch (Exception exception)
{
Logger.Exception(string.Format("SOFTWARE CRITICAL ERROR. Stored Proc Name : {0}", storedProcName), exception);
RetryRequest = false;
throw;
}
}
return result;
}
But my modifications are not perfect. For example, after 3 retry with exception, the code doesn't throw and goes into the continue;
section before going out of the loop.