Skip to content

Commit b534f15

Browse files
authored
Fix | Addressing the excepted behavior when error class is greater than 20 on retry connecting (#1953)
1 parent 7550d41 commit b534f15

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,12 @@ private async Task ReconnectAsync(int timeout)
14161416
if (attempt == retryCount - 1)
14171417
{
14181418
SqlClientEventSource.Log.TryTraceEvent("SqlConnection.ReconnectAsync | Info | Original Client Connection Id {0}, give up reconnection", _originalConnectionId);
1419+
if (e.Class >= TdsEnums.FATAL_ERROR_CLASS)
1420+
{
1421+
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnection.ReconnectAsync|INFO> Original ClientConnectionID {0} - Fatal Error occured. Error Class: {1}", _originalConnectionId, e.Class);
1422+
// Error Class: 20-25, usually terminates the database connection
1423+
InnerConnection.CloseConnection(InnerConnection.Owner, ConnectionFactory);
1424+
}
14191425
throw SQL.CR_AllAttemptsFailed(e, _originalConnectionId);
14201426
}
14211427
if (timeout > 0 && ADP.TimerRemaining(commandTimeoutExpiration) < ADP.TimerFromSeconds(ConnectRetryInterval))

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,12 @@ private async Task ReconnectAsync(int timeout)
17311731
if (attempt == retryCount - 1)
17321732
{
17331733
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnection.ReconnectAsync|INFO> Original ClientConnectionID {0} - give up reconnection", _originalConnectionId);
1734+
if (e.Class >= TdsEnums.FATAL_ERROR_CLASS)
1735+
{
1736+
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnection.ReconnectAsync|INFO> Original ClientConnectionID {0} - Fatal Error occured. Error Class: {1}", _originalConnectionId, e.Class);
1737+
// Error Class: 20-25, usually terminates the database connection
1738+
InnerConnection.CloseConnection(InnerConnection.Owner, ConnectionFactory);
1739+
}
17341740
throw SQL.CR_AllAttemptsFailed(e, _originalConnectionId);
17351741
}
17361742
if (timeout > 0 && ADP.TimerRemaining(commandTimeoutExpiration) < ADP.TimerFromSeconds(ConnectRetryInterval))

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ExceptionTest/ConnectionExceptionTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,38 @@ public class ConnectionExceptionTest
1818
private const string execReaderFailedMessage = "ExecuteReader requires an open and available Connection. The connection's current state is closed.";
1919
private const string orderIdQuery = "select orderid from orders where orderid < 10250";
2020

21+
[Fact]
22+
public void TestConnectionStateWithErrorClass20()
23+
{
24+
using TestTdsServer server = TestTdsServer.StartTestServer();
25+
using SqlConnection conn = new(server.ConnectionString);
26+
27+
conn.Open();
28+
SqlCommand cmd = conn.CreateCommand();
29+
cmd.CommandText = "SELECT 1";
30+
int result = cmd.ExecuteNonQuery();
31+
32+
Assert.Equal(-1, result);
33+
Assert.Equal(System.Data.ConnectionState.Open, conn.State);
34+
35+
server.Dispose();
36+
try
37+
{
38+
int result2 = cmd.ExecuteNonQuery();
39+
}
40+
catch (SqlException ex)
41+
{
42+
Assert.Equal(11, ex.Class);
43+
Assert.NotNull(ex.InnerException);
44+
SqlException innerEx = Assert.IsType<SqlException>(ex.InnerException);
45+
Assert.Equal(20, innerEx.Class);
46+
Assert.StartsWith("A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.", innerEx.Message);
47+
// Since the server is not accessible driver can close the close the connection
48+
// It is user responsibilty to maintain the connection.
49+
Assert.Equal(System.Data.ConnectionState.Closed, conn.State);
50+
}
51+
}
52+
2153
[Fact]
2254
public void ExceptionTests()
2355
{

0 commit comments

Comments
 (0)