Search code examples
c#.netsqldependency

Why is the timeout always a multiple of 15 seconds?


We want to wait for a DB table to change in SQL Server 2022 and therefore use SqlDependency in our C# project. That works great but when we specify the timeout in the constructor of the class, it will always be rounded up to the next 15 seconds. I cannot find anything about that in the documentation.

We are basically doing the following:

var timeoutInSeconds = 5;
var sqlDependency = new SqlDependency(mySimpleSelectCommand, $"Service=mySampleService", timeoutInseconds);
sqlDependency.OnChange += (_, args) => SampleHandler(args);
// execute command etc.

The SampleHandler will be called (as expected) on timeout (= no changes were received). However, that happens after 15 seconds (whereas 5 are specified). When we specify a 20 seconds timeout, it will be called after 30 seconds. To be clear: it is NOT about connection timeouts, it is about the "SqlDependency has not received any changes" timeout.

Is that behavior expected/documented? Can it be changed? It is a bit odd as we'd like to have a different timeout.


Solution

  • Thanks to @Panagiotis Kanavos for digging through the .NET source code and finding the answer:

    As the timer actually checks for the timeout in 15 second intervals, timeouts will effectively always be rounded up to the next multiple of 15 seconds.

    This is odd given that I think a smarter implementation that adapts to the actual timeout that was passed would be pretty easy to implement and at the moment there is no way of passing a timeout less than 15 seconds.