Sqlclient: .NET Core SqlConnection ConnectRetryCount not work?

Created on 12 Oct 2019  路  5Comments  路  Source: dotnet/SqlClient

.NET Core SqlConnection ConnectRetryCount not work?

SqlConnection ConnectRetryCount behaves differently between .NET Framework and .NET Core.

To reproduce

```c#
static string connstr = @"Data Source=localhost;Initial Catalog=master;Integrated Security=True;Connect Timeout=5;Encrypt=False;";

static void Main(string[] args)
{
Test_ConnectRetry();
Console.ReadKey();
}
static void Test_ConnectRetry()
{
Stopwatch s = null;
try
{
using (var conn = new SqlConnection(connstr + "ConnectRetryCount=5;ConnectRetryInterval=5;"))
using (var cmd = new SqlCommand(@"select 1", conn))
{
conn.Open();

        Console.WriteLine(cmd.ExecuteScalar());
        Console.WriteLine("net stop <sql server service>");
        Console.ReadKey(); // manually SQL Server service stop
        Console.WriteLine("SQL Server Service stoped!");
        s = Stopwatch.StartNew();

        cmd.ExecuteScalar();

        Console.WriteLine("not run");
    }
}
catch (Exception ex)
{
    s.Stop();
    Console.WriteLine(ex.Message);
    // .NET Framwork is over 30000ms, .NET Core is about 7000ms
    Console.WriteLine(s.ElapsedMilliseconds);
}

}
```

  • ElapsedMilliseconds

    • .NET Framework 4.7.2 + SqlClient 4.7.0

    • over 30000 milliseconds

    • .NET Core 3.0 + SqlClient 4.7.0

    • about 7000 milliseconds

Expected behavior

Changed ConnectRetryCount value in .NET Core

  • ConnectRetryCount=0

    • thrown exception immediately

  • ConnectRetryCount=1

    • thrown exception after about 7000 milliseconds

  • ConnectRetryCount=5

    • thrown exception after about 7000 milliseconds

ConnectRetryCount not work?

All 5 comments

Have you tried Microsoft.Data.SqlClient?

I tried Microsoft.Data.SqlClient.

  • 1.1.0-preview1.19275.1
  • 1.0.19269.1

execution results of both versions were as follows.

  • ConnectRetryCount=0

    • thrown exception immediately

  • ConnectRetryCount=1

    • thrown exception after about 14000 milliseconds

  • ConnectRetryCount=5

    • thrown exception after about 14000 milliseconds

almost same result ConnectRetryCount=1 and 5.

@OdaShinsuke

Thanks for reporting the issue. The behavior should be same as far as I can tell from implementation goals. We'll update you when we investigate the root cause.

Hi @OdaShinsuke

We recently fixed a bug in Connection Resiliency (#304) that was broken in one of the 4.7.0-preview versions of System.Data.SqlClient and eventually landed in Microsoft.Data.SqlClient.

PR #310 contains the fix for this issue that has been merged, would you like to give it try to check if this is the same problem?

A NuGet package containing this fix can be found here: v1.1.0-build.19320.1-d0672d2

Hi @cheenamalhotra

I tried v1.1.0-build.19320.1-d0672d2. and this problem improved.
Thanks.

Was this page helpful?
0 / 5 - 0 ratings