简体   繁体   English

使用.net MySql连接器无法关闭MySql连接

[英]MySql Connection not closing using .net MySql Connector

MySql Connection is going to sleep mode instead of close in mysql. MySql Connection将进入睡眠模式,而不是在mysql中关闭。 I am using MySql.Data 6.5.4 version to communicate with mysql. 我正在使用MySql.Data 6.5.4版本与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() ... 您可以通过连接字符串参数Pooling=false或静态方法MySqlConnection.ClearPool(connection)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. 当客户端处理MySqlConnection时,连接池通过保持与服务器的本机连接保持活动来工作。 Subsequently, if a new MySqlConnection object is opened, it will be created from the connection pool, rather than creating a new native connection. 随后,如果打开了一个新的MySqlConnection对象,它将从连接池中创建,而不是创建一个新的本机连接。 This improves performance. 这样可以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM