MySql Connection is going to sleep mode instead of close in mysql. I am using MySql.Data 6.5.4 version to communicate with mysql. I am not sure what am I doing wrong in below code.
try
{
using (var conn = new MySqlConnection(strConnection))
{
conn.Open();
using (var cmd = new MySqlCommand(strSQLStatement, conn))
{
var adapter = new MySqlDataAdapter(cmd);
adapter.Fill(ds);
}
}
}
catch (Exception ex)
{
ErrorLogger.WriteError(string.Format("Failed query {0} with stack trace {1} ", strSQLStatement, ex), "GetData");
}
解决方案
Your connection is being added to the connection pool, this feature is on by default so whenever a connection is closed it is added to a connection pool. You can either solve this problem by connection string parameter Pooling=false or the static methods MySqlConnection.ClearPool(connection) and MySqlConnection.ClearAllPools()...
The Connector/Net supports connection pooling for better performance and scalability with database-intensive applications. This is enabled by default. You can turn it off or adjust its performance characteristics using the connection string options Pooling, Connection Reset, Connection Lifetime, Cache Server Properties, Max Pool Size and Min Pool Size.
Connection pooling works by keeping the native connection to the server live when the client disposes of a MySqlConnection. Subsequently, if a new MySqlConnection object is opened, it will be created from the connection pool, rather than creating a new native connection. This improves performance.