-
Notifications
You must be signed in to change notification settings - Fork 315
Description
We have a high-load service which we want to run robustly when SQL server becomes (temporarily) unavailable. To achieve this, we set Connection Timeout (and Command Timeout as well, but that is not important for the purpose of this issue). We then expect connection attempt fail fast if the SQL server is not available. This way, our server will be able to do its job even if the SQL server is down for some time. But, somehow this does not work as expected. Following code emulates our multithreaded server attempting to connect to an (unavailable) SQL server from multiple threads a time:
using System.Diagnostics;
using Microsoft.Data.SqlClient;
const string connectionString = "Server=tcp:noserver.database.windows.net,1433;Database=nodb;User=nouser;Password=nopassword;Connection Timeout=3";
const int numberOfTasks = 1000;
var tasks = new Task[numberOfTasks];
for (var i = 0; i < numberOfTasks; ++i)
{
tasks[i] = Task.Run(async () =>
{
var timer = Stopwatch.StartNew();
try
{
await using var conn = new SqlConnection(connectionString);
await conn.OpenAsync();
}
catch (Exception)
{
Console.WriteLine("Connection failed after {0} sec", timer.Elapsed.TotalSeconds);
}
});
}
Task.WaitAll(tasks);
When this code is run outputs something like:
Connection failed after 0.5961428 sec
Connection failed after 0.901156 sec
Connection failed after 1.2156149 sec
...
Connection failed after 30.7706116 sec
...
Connection failed after 56.6531985 sec
Connection failed after 56.9537902 sec
....
So, clearly Connection Timeout is not being respected. This causes our server to stall completely when SQL server is down. This is not the way we want it to work :). Any idea why we are observing this behavior?
Metadata
Metadata
Assignees
Labels
Type
Projects
Status