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 4b4e7ccc43..5eed2f7177 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 @@ -1940,7 +1940,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 || @@ -1969,7 +1969,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 b6104b9075..8fca4ce207 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 @@ -2144,7 +2144,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 || @@ -2187,7 +2187,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 8f16c09aa3..93dbc15ad7 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs @@ -10,8 +10,6 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Security; -using System.Threading; -using System.Threading.Tasks; using Microsoft.SqlServer.TDS.Servers; using Xunit; @@ -57,26 +55,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(TestUtility), nameof(TestUtility.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(TestUtility), nameof(TestUtility.IsNotArmProcess))] [InlineData(40613)] [InlineData(42108)] @@ -100,54 +78,14 @@ public void TransientFaultTest(uint errorCode) } catch (Exception e) { - Assert.Fail(e.Message); + if (null != connection) + { + Assert.Equal(ConnectionState.Closed, connection.State); + } + Assert.False(true, e.Message); } } - [ConditionalTheory(typeof(TestUtility), nameof(TestUtility.IsNotArmProcess))] - [InlineData(40613)] - [InlineData(42108)] - [InlineData(42109)] - [PlatformSpecific(TestPlatforms.Windows)] - public void 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); - Task e = Assert.ThrowsAsync(async () => await connection.OpenAsync()); - Assert.Equal(20, e.Result.Class); - Assert.Equal(ConnectionState.Closed, connection.State); - } - - [ConditionalTheory(typeof(TestUtility), nameof(TestUtility.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); - SqlException e = Assert.Throws(() => connection.Open()); - 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; } } }