gpt4 book ai didi

c# - MySql Connection 未使用 .net MySql Connector 关闭

转载 作者:行者123 更新时间:2023-11-29 01:40:41 34 4
gpt4 key购买 nike

MySql 连接将进入休眠模式,而不是在 mysql 中关闭。我正在使用 MySql.Data 6.5.4 版本与 mysql 进行通信。我不确定我在下面的代码中做错了什么。

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

最佳答案

您的连接正在被添加到连接池中,此功能默认情况下处于启用状态,因此无论何时关闭连接,它都会被添加到连接池中。您可以通过连接字符串参数 Pooling=false 或静态方法 MySqlConnection.ClearPool(connection)MySqlConnection.ClearAllPools() 解决此问题...

来自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.

关于c# - MySql Connection 未使用 .net MySql Connector 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24436920/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com