2

I have a very basic C# console app that connects to a db, executes a query, closes the connection, and exits out of the app.

The problem is, the app takes almost 3 seconds to exit.

I have displayed the time at each step to see why it is running slowly and it isn't during any of the processing, just when it exits out of the app.

Does anyone know how to speed this up?

Here is the output:

Opening Connection:94ms
26:OK
Closing Connection:356ms
Closed Connection:357ms
Exiting:358ms
[Delay of about 3 seconds before it exits]

And here is the code:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace CheckSQL
{
    class Program
    {
        static Stopwatch watch = new Stopwatch();

        static void Main(string[] args)
        {
            if (args.Length == 0) return;
            watch.Start();

            string connstring = args[0];
            string sqlquery = args[1];

            ExecuteScalar(connstring, sqlquery);
            watch.Stop();
            Console.WriteLine(string.Format("Exiting:{0}ms", watch.ElapsedMilliseconds));
        }

        private static void ExecuteScalar(string connstring, string sqlquery)
        {
            SqlConnection sqlconn = new SqlConnection(connstring);
            SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlconn);

            try
            {
                Console.WriteLine(string.Format("Opening Connection:{0}ms", watch.ElapsedMilliseconds));
                sqlconn.Open();
                Console.WriteLine(string.Format("{0}:OK", sqlcmd.ExecuteScalar()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("0:{0}", ex.Message));
            }
            finally
            {
                if (sqlconn.State == ConnectionState.Open)
                {
                    Console.WriteLine(string.Format("Closing Connection:{0}ms", watch.ElapsedMilliseconds));
                    sqlconn.Close();
                    Console.WriteLine(string.Format("Closed Connection:{0}ms", watch.ElapsedMilliseconds));
                }
            }
        }
    }
}
ITManx Ltd
  • 43
  • 1
  • 5
  • Is it also that slow when running without debugging? It may be that it's just detaching the debugger that's taking the time... – Jaymz May 04 '11 at 09:21
  • Unfortunately thats from the release exe, not the debugger. – ITManx Ltd May 04 '11 at 09:22
  • watch is just the stopwatch timer... it is only used to show the running time. It is just as slow without it in there. [update:... ok, he removed his comment :)] – ITManx Ltd May 04 '11 at 09:24
  • 2
    How did you get the 3 second delay? – hallie May 04 '11 at 09:27
  • I'm assuming you mean when it writes 'Exiting', that it takes 3 seconds AFTER that before it closes? Because your code executes in 0.3 second. – Phill May 04 '11 at 09:34
  • Hallie - It's when it closes the app. I think Henk (comment below) is correct in that it has to do some housekeeping but there must be a way of speeding it up.. – ITManx Ltd May 04 '11 at 09:36
  • Phill - Exactly. Once it's done all the database work and console writing, it then takes 3 seconds to return to the C:\ prompt. – ITManx Ltd May 04 '11 at 09:38

3 Answers3

3

I had a similar problem with a C# Console app, and found that the issue had something to do with the cleanup of the Connection Pool when the app exits. With connections in the pool, I measured a 1.6 second delay in exiting (Timed by an external script calling my EXE). Although I wasn't entirely happy with the solution, I found that issuing the following, before exiting removed the delay:

System.Data.SqlClient.SqlConnection.ClearAllPools();

I would guess that using "Pooling=False", in your connection strings would also do the trick... But you would only do that if you didn't need the benefits of pooling.

  • 1
    This is the exact cause of a similar issue I am experiencing, calling `System.Data.SqlClient.SqlConnection.ClearAllPools();` removes the delay. – Jamezor Jan 15 '18 at 21:46
1

Closing a connection (calling sqlconn.Close() ) only means returning it to the ConnectionPool.

So there still is some housekeeping to be done on exit.

3 seconds seems a bit long, but there are several components (CLR, Database) in play here.

H H
  • 263,252
  • 30
  • 330
  • 514
0

I think its impossible to do in correct way. How can you seepd up job which takes some time? The only possible way in this case - to optimize algorythm, but you cant do this. As I understand you should return control immediately after checking some database information. You can workaround this by creating two process system. First process would startup second and the second in turn would check infomation in database and send results to the first process which would communicate with user. So you alway would return control after retrieving results. The second process would take some time to terminate but this fact should not worry you because you would have control by that moment.

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69