From 3dfc04dde9e91be5c282d3eb92dff3455aa8a6c7 Mon Sep 17 00:00:00 2001 From: Charles Gagnon Date: Tue, 30 May 2023 08:48:05 -0700 Subject: [PATCH] Add additional check for closed/broken connection errors (#862) --- src/SqlBindingUtilities.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/SqlBindingUtilities.cs b/src/SqlBindingUtilities.cs index 1882666e..f2c30435 100644 --- a/src/SqlBindingUtilities.cs +++ b/src/SqlBindingUtilities.cs @@ -250,8 +250,16 @@ internal static async Task OpenAsyncWithSqlErrorHandling(this SqlConnection conn /// True if the exception is a fatal SqlClientException, false otherwise 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"))); } ///