1

Is there an effective way to continuously check if a connection to the database is established? For example, if the network drops or the server computer is turned off, then there is no obvious database available. My idea was to create a Timer that would poll every 10 seconds or so to determine an available connection but that just seems like another "hack". Another idea is to check connectivity before any user input. Here is the sample code that would check for a connection:

    public bool isDbAvail()
    {
        using (MySqlConnection conn = new MySqlConnection(TimeClock.Properties.Settings.Default.timeclockConnectionString))
        {
            try
            {
                conn.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
    }

Any input would be great on what would be the best approach.

slugster
  • 49,403
  • 14
  • 95
  • 145
Dimitri
  • 1,906
  • 3
  • 25
  • 49
  • Are you trying to see if there's a connection established as stated in your first sentence, or are you trying to see if you *can* establish a connection as shown in your code snippet? – itsme86 Jul 07 '14 at 21:24
  • 1
    Why do you need this continuous check? It seems a waste of resources to continuosly poll the availability of the remote database. – Steve Jul 07 '14 at 21:31
  • @Steve: it depends - if you need to perform something quickly you better have already established connection. So there might be cases when polling worth it. – zerkms Jul 07 '14 at 21:32
  • There is no way other than a 'hack' because you are not __supposed__ to keep a connection open. Period. You can test quickly by Ping'ing but you should not even try to keep the connection open all the time. Of course it is up to you to break the rules, but any way to do that qualifies as a hack, imo. (BTW: I'm ready to consider/do it, too, but only because the the DB is local and single user..) – TaW Jul 07 '14 at 21:40
  • @TaW I'm not trying to keep the connection opened. I simply want to see if it's possible to connect. The application is used for employees to punch in and out with their punch cards. If no connection can be established, then no one can punch in and the application behaves accordingly with a message saying so. Also, if the connection is re-established, then punching in and out would be permitted again. – Dimitri Jul 07 '14 at 21:44
  • Well, OK. But beyond pinging the server you can't do anything without actually connecting, I believe. Possibly a server app could check to see if the DB is running..? – TaW Jul 07 '14 at 21:48
  • Hm, how many instances will be doing this? One per employee or one per punch clock? Keeping __one__ (or __very__ few) connection(s) open is not really a problem, imo. – TaW Jul 07 '14 at 21:51
  • @TaW it would be one per punch clock in the shop that all employees use. It seems this is a two-part problem. The physical aspect which can be dealt with a simple ping and the other aspect is the database availability... – Dimitri Jul 07 '14 at 21:54
  • You can ping the database itself, look [here](http://dev.mysql.com/doc/refman/5.0/en/mysql-ping.html) and also at this [post](http://stackoverflow.com/questions/2546868/cheapest-way-to-to-determine-if-a-mysql-connection-is-still-alive). Go for your 'hack', modfied to ping; imo, for this situation it seems to be the right thing. – TaW Jul 07 '14 at 21:58
  • @TaW Yea, I had a feeling it was going to boil down to that. Thanks for the help though. – Dimitri Jul 08 '14 at 00:33

0 Answers0