broadband
broadband

Reputation: 3488

When .NET runtime closes (My)SQL connections from connection pool

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 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).

Upvotes: 0

Views: 1353

Answers (1)

Kami
Kami

Reputation: 19407

From the MSDN link you have specified

If the specified MinPoolSize is greater than zero, the connection pool is not destroyed until the AppDomain is unloaded and the process ends.

Further

The pool is automatically cleared when a fatal error occurs, such as a failover.

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.

Upvotes: 1

Related Questions