Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix | Addressing the excepted behavior when error class is greater than 20 on retry connecting #1953

Merged
merged 5 commits into from Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1414,6 +1414,11 @@ private async Task ReconnectAsync(int timeout)
if (attempt == retryCount - 1)
{
SqlClientEventSource.Log.TryTraceEvent("SqlConnection.ReconnectAsync | Info | Original Client Connection Id {0}, give up reconnection", _originalConnectionId);
if (e.Class >= TdsEnums.FATAL_ERROR_CLASS)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnection.ReconnectAsync|INFO> Original ClientConnectionID {0} - Fatal Error occured. Error Class: {1}", _originalConnectionId, e.Class);
InnerConnection.CloseConnection(InnerConnection.Owner, ConnectionFactory);
}
throw SQL.CR_AllAttemptsFailed(e, _originalConnectionId);
}
if (timeout > 0 && ADP.TimerRemaining(commandTimeoutExpiration) < ADP.TimerFromSeconds(ConnectRetryInterval))
Expand Down
Expand Up @@ -1728,6 +1728,11 @@ private async Task ReconnectAsync(int timeout)
if (attempt == retryCount - 1)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnection.ReconnectAsync|INFO> Original ClientConnectionID {0} - give up reconnection", _originalConnectionId);
if (e.Class >= TdsEnums.FATAL_ERROR_CLASS)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlConnection.ReconnectAsync|INFO> Original ClientConnectionID {0} - Fatal Error occured. Error Class: {1}", _originalConnectionId, e.Class);
InnerConnection.CloseConnection(InnerConnection.Owner, ConnectionFactory);
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
}
throw SQL.CR_AllAttemptsFailed(e, _originalConnectionId);
}
if (timeout > 0 && ADP.TimerRemaining(commandTimeoutExpiration) < ADP.TimerFromSeconds(ConnectRetryInterval))
Expand Down
Expand Up @@ -18,6 +18,31 @@ public class ConnectionExceptionTest
private const string execReaderFailedMessage = "ExecuteReader requires an open and available Connection. The connection's current state is closed.";
private const string orderIdQuery = "select orderid from orders where orderid < 10250";

[Fact]
public void TestConnectionStateWithErrorClass20()
{
using TestTdsServer server = TestTdsServer.StartTestServer();
using SqlConnection conn = new(server.ConnectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT 1";
int result = cmd.ExecuteNonQuery();
Assert.Equal(-1, result);
Assert.Equal("Open", conn.State.ToString());
server.Dispose();
try
{
int result2 = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Assert.True(ex.Class >= 20);
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
// Since the server is not accessible driver can close the close the connection
// It is user responsibilty to maintain the connection.
Assert.Equal("Closed", conn.State.ToString());
}
}

[Fact]
public void ExceptionTests()
{
Expand Down