Bug 42804 - Concurrent SqlCommand queries causing Timeout Exception
Summary: Concurrent SqlCommand queries causing Timeout Exception
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data (show other bugs)
Version: unspecified
Hardware: Other Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-07-26 14:08 UTC by waynemetcalfe
Modified: 2018-02-22 22:22 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description waynemetcalfe 2016-07-26 14:08:09 UTC
We have an ASP.NET web service written in C# and compiled and running in mono 4.4.1. It's a pretty typical web service, serving up results from a sql server database.

Occasionally there are some long running queries (a few minutes) and we noticed some timeout exceptions in the logs, specifically:

    Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

In our setup the timeout for queries had been set for 5 minutes, but these exceptions were being thrown before that point was reached. After some investigation we discovered that if the web service handled a few short running queries while a long running query was running, the above timeout exception would be thrown for the long running query. This was not seen when running in .NET.

In an attempt to simplify the problem, the below application was created. It runs a 5 minute query on a background thread and 200, 2 second queries sequentially in the foreground.

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.Common;
    using System.Threading;
    
    
    
    namespace SqlCommandTest
    {
        class MainClass
        {
            public static void Main (string[] args)
            {
                var runShort = true;
    
                var connstr = "Data Source=xxxxx;UID=xxxxx;PWD=xxxxx;Initial Catalog=testdbA;pooling=false";
                var connstr2 = "Data Source=xxxxx;UID=xxxxx;PWD=xxxxx;Initial Catalog=testdbB;pooling=false";
    
                var t1 = new Thread (() => 
                {
                    Thread.CurrentThread.IsBackground = true;
    
                    SqlClientFactory factory = System.Data.SqlClient.SqlClientFactory.Instance;
                    var conn = factory.CreateConnection ();
                    conn.ConnectionString = connstr;
                    conn.Open ();
                    using (System.Data.Common.DbCommand command = factory.CreateCommand ()) {
                        command.CommandTimeout = 400;
                        command.Connection = conn;
                        command.CommandText = "waitfor delay '00:05';";
                        Console.WriteLine("Running long report!!!!!!!!!!!!!!!");
                        var watch = System.Diagnostics.Stopwatch.StartNew();
                        using (DbDataReader reader = command.ExecuteReader ()) {
                            watch.Stop();
                            Console.WriteLine ("Long Report finished after " + watch.ElapsedMilliseconds/1000 + " seconds");
                        }
                    }
                    conn.Close ();
                });
                t1.Start ();
    
                if (runShort) {
                    
                    for (int i = 0; i < 200; i++) {
                        SqlClientFactory factory = System.Data.SqlClient.SqlClientFactory.Instance;
                        var conn = factory.CreateConnection ();
                        conn.ConnectionString = connstr2;
                        conn.Open ();
                        using (System.Data.Common.DbCommand command = factory.CreateCommand ()) {
                            command.CommandTimeout = 400;
                            command.Connection = conn;
                            command.CommandText = "waitfor delay '00:00:02';";
                            Console.WriteLine ("Running Short report " + i + " !");
                            using (DbDataReader reader = command.ExecuteReader ()) {
                                Console.WriteLine ("Short Report " + i + " finished");
                            }
                        }
                        conn.Close ();
                    }
                }
    
                //wait for t1 to finish
                t1.Join ();
            }
        }
    }

After about 75 of the short queries the following error is produced:
    
    System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
      at System.Data.SqlClient.SqlCommand.Execute (Boolean wantResults) [0x00234] in /tmp/buildd/mono-4.4.1.0/mcs/class/System.Data/System.Data.SqlClient/SqlCommand.cs:446 
      at System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior) [0x00035] in /tmp/buildd/mono-4.4.1.0/mcs/class/System.Data/System.Data.SqlClient/SqlCommand.cs:501

Disabling the running of the short queries by setting `var runShort = false` enables the long report to run for 5 minutes as expected.

It should be noted that connection pooling is turned off to workaround this [issue][1].

Has anyone come across this issue before or have any suggestions for a workaround?


  [1]: http://stackoverflow.com/questions/37446935/how-to-avoid-mono-ado-net-serving-wrong-results-to-requests-after-one-times-out
Comment 1 Marek Safar 2018-02-22 22:22:52 UTC
Mono 5.10 has significantly improved System.Data implementation which should resolve this issue. If you can still reproduce it please reopen the issue.