MySql Connection not closing using .net MySql Connector

耗尽温柔 提交于 2019-12-11 03:38:38

问题


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");
        }

回答1:


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

From Official Documentation

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.



来源:https://stackoverflow.com/questions/24436920/mysql-connection-not-closing-using-net-mysql-connector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!