From d6b8f779286d6e4a0c4e8027d18af6565779483e Mon Sep 17 00:00:00 2001 From: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com> Date: Fri, 17 May 2024 11:55:23 -0700 Subject: [PATCH] [5.1.6] | Revert the fix transient fault handling issue with OpenAsync (#1983) (#2508) --- .../Microsoft/Data/SqlClient/SqlConnection.cs | 4 +- .../Microsoft/Data/SqlClient/SqlConnection.cs | 4 +- .../SqlConnectionBasicTests.cs | 84 +------------------ .../TDS.Servers/TransientFaultTDSServer.cs | 1 - 4 files changed, 8 insertions(+), 85 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs index 1040061b59..660b5934e0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -1820,7 +1820,7 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec } } - _applyTransientFaultHandling = (!overrides.HasFlag(SqlConnectionOverrides.OpenWithoutRetry) && connectionOptions != null && connectionOptions.ConnectRetryCount > 0); + _applyTransientFaultHandling = (!overrides.HasFlag(SqlConnectionOverrides.OpenWithoutRetry) && retry == null && connectionOptions != null && connectionOptions.ConnectRetryCount > 0); if (connectionOptions != null && (connectionOptions.Authentication == SqlAuthenticationMethod.SqlPassword || @@ -1849,7 +1849,7 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec // does not require GC.KeepAlive(this) because of ReRegisterForFinalize below. // Set future transient fault handling based on connection options - _applyTransientFaultHandling = connectionOptions != null && connectionOptions.ConnectRetryCount > 0; + _applyTransientFaultHandling = (retry == null && connectionOptions != null && connectionOptions.ConnectRetryCount > 0); var tdsInnerConnection = (SqlInternalConnectionTds)InnerConnection; diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs index 1ab55969a0..04acfcb612 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -2059,7 +2059,7 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec bool result = false; - _applyTransientFaultHandling = (!overrides.HasFlag(SqlConnectionOverrides.OpenWithoutRetry) && connectionOptions != null && connectionOptions.ConnectRetryCount > 0); + _applyTransientFaultHandling = (!overrides.HasFlag(SqlConnectionOverrides.OpenWithoutRetry) && retry == null && connectionOptions != null && connectionOptions.ConnectRetryCount > 0); if (connectionOptions != null && (connectionOptions.Authentication == SqlAuthenticationMethod.SqlPassword || @@ -2102,7 +2102,7 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec } // Set future transient fault handling based on connection options - _applyTransientFaultHandling = connectionOptions != null && connectionOptions.ConnectRetryCount > 0; + _applyTransientFaultHandling = (retry == null && connectionOptions != null && connectionOptions.ConnectRetryCount > 0); return result; } diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs index 2ebf172786..e3183ee6a0 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs @@ -54,26 +54,6 @@ public async Task PreLoginEncryptionExcludedTest() Assert.Contains("The instance of SQL Server you attempted to connect to does not support encryption.", ex.Message, StringComparison.OrdinalIgnoreCase); } - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArmProcess))] - [InlineData(40613)] - [InlineData(42108)] - [InlineData(42109)] - [PlatformSpecific(TestPlatforms.Windows)] - public async Task TransientFaultTestAsync(uint errorCode) - { - using TransientFaultTDSServer server = TransientFaultTDSServer.StartTestServer(true, true, errorCode); - SqlConnectionStringBuilder builder = new() - { - DataSource = "localhost," + server.Port, - IntegratedSecurity = true, - Encrypt = SqlConnectionEncryptOption.Optional - }; - - using SqlConnection connection = new(builder.ConnectionString); - await connection.OpenAsync(); - Assert.Equal(ConnectionState.Open, connection.State); - } - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArmProcess))] [InlineData(40613)] [InlineData(42108)] @@ -97,70 +77,14 @@ public void TransientFaultTest(uint errorCode) } catch (Exception e) { + if (null != connection) + { + Assert.Equal(ConnectionState.Closed, connection.State); + } Assert.False(true, e.Message); } } - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArmProcess))] - [InlineData(40613)] - [InlineData(42108)] - [InlineData(42109)] - [PlatformSpecific(TestPlatforms.Windows)] - public async Task TransientFaultDisabledTestAsync(uint errorCode) - { - using TransientFaultTDSServer server = TransientFaultTDSServer.StartTestServer(true, true, errorCode); - SqlConnectionStringBuilder builder = new() - { - DataSource = "localhost," + server.Port, - IntegratedSecurity = true, - ConnectRetryCount = 0, - Encrypt = SqlConnectionEncryptOption.Optional - }; - - using SqlConnection connection = new(builder.ConnectionString); - try - { - await connection.OpenAsync(); - Assert.False(true, "Connection should not have opened."); - } - catch (SqlException e) - { - // FATAL Error, should result in closed connection. - Assert.Equal(20, e.Class); - Assert.Equal(ConnectionState.Closed, connection.State); - } - } - - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArmProcess))] - [InlineData(40613)] - [InlineData(42108)] - [InlineData(42109)] - [PlatformSpecific(TestPlatforms.Windows)] - public void TransientFaultDisabledTest(uint errorCode) - { - using TransientFaultTDSServer server = TransientFaultTDSServer.StartTestServer(true, true, errorCode); - SqlConnectionStringBuilder builder = new() - { - DataSource = "localhost," + server.Port, - IntegratedSecurity = true, - ConnectRetryCount = 0, - Encrypt = SqlConnectionEncryptOption.Optional - }; - - using SqlConnection connection = new(builder.ConnectionString); - try - { - connection.Open(); - Assert.False(true, "Connection should not have opened."); - } - catch (SqlException e) - { - // FATAL Error, should result in closed connection. - Assert.Equal(20, e.Class); - Assert.Equal(ConnectionState.Closed, connection.State); - } - } - [Fact] public void SqlConnectionDbProviderFactoryTest() { diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/TransientFaultTDSServer.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/TransientFaultTDSServer.cs index eda4de8e2a..419f7e5d24 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/TransientFaultTDSServer.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/TransientFaultTDSServer.cs @@ -146,7 +146,6 @@ private void Dispose(bool isDisposing) if (isDisposing) { _endpoint?.Stop(); - RequestCounter = 0; } } }