title: When .NET runtime closes (My)SQL connections from connection pool
body: <p>I recently read about connection pooling (<a href="http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx" rel="nofollow">msdn</a>, <a href="http://dev.mysql.com/doc/connector-net/en/connector-net-programming-connection-pooling.html" rel="nofollow">mysql documentation</a>).</p>
  
  <p>The aim of a Connection Pool is that the MySQL clients should not be forced to constantly do connect and disconnect. Thus it is possible to cache a connection in the MySQL client when a user of the connection no longer needs it. Thus another user that needs a connection to the same MySQL Server can reuse this cached connection later on (by Mikael Ronstrom).</p>
  
  <p>What this means is:</p>
  
  <ul>
  <li>I connect to MySQL server</li>
  <li>execute some queries</li>
  <li>call MySqlConnection.Close()</li>
  <li>at this point connection is not closed, it is kept in connection pool of the client (application). If I execute again MySqlConnection.Open(connection string), connection will be created/taken from the connection pool of the application (connection poll just returns instance of MySqlConnection).</li>
  </ul>
  
  <p>I tested with this code:</p>
  
  <pre><code> MySqlConnection conn = new MySqlConnection("server=localhost;uid=user;pwd=pass;database=dbname;Pooling=true");
  
   try
   {
     conn.Open();
     Console.WriteLine("Connected to sql server");
   }
   catch (MySqlException e)
   {
     Console.WriteLine("Error connecting to sql server ["+e.Message+"]");
   }
  
   MySqlCommand cmd = new MySqlCommand("select * from table1 where id = 1", conn);
   MySqlDataReader reader = cmd.ExecuteReader();
  
   while (reader.Read())
   {
     string a = reader.GetString(0);
   }
   reader.Close();
  
   try
   {
     conn.Close();
   }
   catch (MySqlException e) {}
  
   // I connected to MySQL server via terminal and executed 'show full processlist'
   // connection was still present
   // If I add MySqlConnection.ClearAllPools(); connection was closed (everything as it should)
  </code></pre>
  
  <p>At which point does .NET runtime call connector MySqlConnection.ClearAllPools(); or MySqlConnection.ClearPool() static methods? These methods actually close connection with MySQL server. What happens in case .NET application crashes, does MySQL connection gets closed after wait_timeout has passed. Should I call ClearAllPool() at the end of application execution (last line).</p>