1
Vote

SqlDatabaseTransientErrorDetectionStrategy which may not detect transient faults under certain conditions

description

A few customers ran into the issue with SqlDatabaseTransientErrorDetectionStrategy which may not detect transient faults under certain conditions. This is because some SQL exceptions do not carry the Errors collection, and the last error code is reported directly at the SqlException object level.

Essentially, you cannot just do this:
foreach (SqlError error in sqlException.Errors)
         {
           switch (error.Number)
           {
             case 40501:
               ThrottlingCondition throttlingCondition = ThrottlingCondition.FromError(error);
               sqlException.Data[(object) throttlingCondition.ThrottlingMode.GetType().Name] = (object) ((object) throttlingCondition.ThrottlingMode).ToString();
               sqlException.Data[(object) throttlingCondition.GetType().Name] = (object) throttlingCondition;
               return true;
             case 40540:
             case 40613:
             case 10928:
             case 10929:
             case 40143:
             case 40197:
             case 233:
             case 10053:
             case 10054:
             case 10060:
             case 20:
             case 64:
               return true;
             default:
               continue;
           }
         }
Instead, you should also include the Number property on the SqlException object as certain SQL faults do not come back with the Errors collection at all. The possible implementation would look as follows:
// Collect all errors into a single enumerable sequence.
                    var sqlErrorCodes = new List<int> { sqlException.Number };
                    sqlErrorCodes.AddRange(sqlException.Errors.Cast<SqlError>().Select(err => err.Number));
 
                    // Enumerate through all error codes found in the exception.
                    foreach (var errorCode in sqlErrorCodes)
                    {
                        switch (errorCode)
                        {
                            // SQL Error Code: 40501
                                                //…

comments