Created
June 7, 2016 03:41
-
-
Save kendallmiller/dddb53f0b0401d564440066d9e285a6e to your computer and use it in GitHub Desktop.
ADO.NET SQL Server error category resolver for SQL Server and SQL Azure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Examines exceptions for possible retry scenarios. | |
| /// </summary> | |
| public static DatabaseErrorCategory ResolveErrorCategory(SqlException ex) | |
| { | |
| bool isConnectionError = false; | |
| bool isDeadlockError = false; | |
| bool isTimeoutError = false; | |
| foreach (SqlError error in ex.Errors) | |
| { | |
| switch (error.Number) | |
| { | |
| case 20: //The instance of SQL Server you attempted to connect to does not support encryption. (PMcE: amazingly, this is transient) | |
| case 64: //A connection was successfully established with the server, but then an error occurred during the login process. | |
| case 233: //The client was unable to establish a connection because of an error during connection initialization process before login | |
| case 10053: //transport level error | |
| case 10054: //transport level error | |
| case 10060: //network or instance error | |
| case 10061: //network or instance error | |
| case 10928: //Azure out of resources | |
| case 10929: //Azure out of resources | |
| case 40143: //connection could not be initialized | |
| case 40197: //the service encountered an error | |
| case 40501: //service is busy | |
| case 40613: //database unavailable | |
| isConnectionError = true; | |
| break; | |
| case 3960: //snapshot isolation error | |
| case 1205: //deadlock | |
| isDeadlockError = true; | |
| break; | |
| case -2: //timeout | |
| isTimeoutError = true; | |
| break; | |
| case 50000: //explicit TSQL RAISERROR | |
| //see if this is a rethrow if a deadlock.. | |
| if (error.Message.Contains("Error 1205")) | |
| { | |
| isDeadlockError = true; | |
| } | |
| break; | |
| } | |
| } | |
| DatabaseErrorCategory category = DatabaseErrorCategory.Unknown; | |
| //the order we check these sets our priority of handling them. | |
| if (isDeadlockError) | |
| category = DatabaseErrorCategory.Deadlock; | |
| else if (isConnectionError) | |
| category = DatabaseErrorCategory.Connection; | |
| else if (isTimeoutError) | |
| category = DatabaseErrorCategory.Timeout; | |
| return category; | |
| } | |
| /// <summary> | |
| /// The type of error we experienced | |
| /// </summary> | |
| [Flags] | |
| public enum DatabaseErrorCategory | |
| { | |
| /// <summary> | |
| /// No recognized error type was determined | |
| /// </summary> | |
| Unknown = 0, | |
| /// <summary> | |
| /// The operation timed out | |
| /// </summary> | |
| Timeout = 1, | |
| /// <summary> | |
| /// The connection failed in some way | |
| /// </summary> | |
| Connection = 2, | |
| /// <summary> | |
| /// The operation failed due to DB deadlock | |
| /// </summary> | |
| Deadlock = 4, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment