title: How to check for database availability
body: <p>I have the following code to test DB connection, it runs periodically to test for DB availability:</p>
  <pre><code>private bool CheckDbConn()
  {
     SqlConnection conn = null;
     bool result = true;
  
     try
     {
         conn = DBConnection.getNewCon();
         ConnectionState conState = conn.State;
  
         if (conState == ConnectionState.Closed || conState == ConnectionState.Broken)
         {
            logger.Warn(LogTopicEnum.Agent, &quot;Connection failed in DB connection test on CheckDBConnection&quot;);
            return false;
         }             
     }
     catch (Exception ex)
     {
        logger.Warn(LogTopicEnum.Agent, &quot;Error in DB connection test on CheckDBConnection&quot;, ex);
        return false; // any error is considered as db connection error for now
     }
     finally
     {
        try
        {
           if (conn != null)
           {
              conn.Close();
           }
        }
        catch (Exception ex)
        {
           logger.Warn(LogTopicEnum.Agent, &quot;Error closing connection on CheckDBConnection&quot;, ex);
           result = false;
        }
     }
     return result;
  }
  </code></pre>
  <p>And:</p>
  <pre><code>static public SqlConnection getNewCon()
  {
      SqlConnection newCon = new SqlConnection();
      newCon.ConnectionString = DBConnection.ConnectionString; // m_con.ConnectionString;
      newCon.Open();
      return newCon;
  }
  </code></pre>
  <p>My question is: will this work as expected?</p>
  <p>Specifically, I'm concerned about the test of the <code>ConnectionState</code>. Is it possible that the state will be: connecting (since <code>Open()</code> is synchronous)?</p>
  <p>What should I do in that case?</p>