Bug 42804 - Concurrent SqlCommand queries causing Timeout Exception
Summary: Concurrent SqlCommand queries causing Timeout Exception
Status: NEW
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: 2017-09-01 09:30 UTC (History)
1 user (show)

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


Attachments

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

Note You need to log in before you can comment on or make changes to this bug.