Skip to content

Commit

Permalink
Add additional check for closed/broken connection errors (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Gagnon authored May 30, 2023
1 parent 18a16a2 commit 3dfc04d
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/SqlBindingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,16 @@ internal static async Task OpenAsyncWithSqlErrorHandling(this SqlConnection conn
/// <returns>True if the exception is a fatal SqlClientException, false otherwise</returns>
internal static bool IsFatalSqlException(this Exception e)
{
string lowerMessage = e.Message.ToLowerInvariant();
// Most SqlExceptions wrap the original error from the native driver, so make sure to check both
return (e as SqlException)?.Class >= 20 || (e.InnerException as SqlException)?.Class >= 20;
return (e as SqlException)?.Class >= 20
|| (e.InnerException as SqlException)?.Class >= 20
// TEMPORARY - Not all exceptions thrown by SqlClient are SqlExceptions, and the current SqlClient
// does not correctly update the State property in all cases. So for now explicitly check for
// the string containing a message indicating that the connection has been closed or broken.
// This should be removed once version 5.2.0+ has been released
// https://github.com/Azure/azure-functions-sql-extension/issues/860
|| (lowerMessage.Contains("connection") && (lowerMessage.Contains("broken") || lowerMessage.Contains("closed")));
}

/// <summary>
Expand Down

0 comments on commit 3dfc04d

Please sign in to comment.