I recently read about connection pooling (msdn, mysql documentation).
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).
What this means is:
- I connect to MySQL server
- execute some queries
- call MySqlConnection.Close()
- 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).
I tested with this 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)
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).
From the MSDN link you have specified
Further
As such, the connection would be cleaned up when the application exits.
You should be able to test this by making a connection, verifying it is still open after using the
close()
method and then causing the application crash.