gpt4 book ai didi

c# - SqlConnection 池似乎不起作用

转载 作者:行者123 更新时间:2023-11-30 20:40:31 27 4
gpt4 key购买 nike

我正在尝试优化访问 SQL Server 2014 数据库的代码性能,并注意到连接池似乎并不像 ADO.NET 所宣传的那样工作。默认情况下,它应该被启用并且开箱即用(只要使用相同的连接字符串)。然而,我的实验表明,打开/关闭 SqlConnection 上的连接实际上会导致引发审核登录/注销。

根据 https://msdn.microsoft.com/en-us/library/vstudio/8xx3tyca(v=vs.100).aspx情况不应该是这样的:

Login and logout events will not be raised on the server when a connection is fetched from or returned to the connection pool. This is because the connection is not actually closed when it is returned to the connection pool.

我的简单测试是创建一个控制台应用程序,类似于以下内容:

var conn = new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=icedb;");

for (int i = 0; i < 3; i++)
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select 1";
cmd.ExecuteScalar();
conn.Close();
}

运行此代码会产生 Sql Profiler 捕获的结果,类似于以下内容。观察多个登录/注销事件,如果池按照宣传的那样工作,则不应捕获这些事件。我已经能够在多台计算机(Windows 8/10、.NET 4.5、Sql Server 2014 Developer Edition)上重现这一点。

enter image description here

主要问题:如何让连接池发挥作用。

最佳答案

如果发出sp_reset_connection,则启用连接池。请参阅What does sp_reset_connection do?

要查看审核登录在分析器中是池化还是非池化,您需要将 EventSubClass 列添加到跟踪并检查审核登录和审核注销事件。新列将显示“1 - Nonpooled”或“2 - Pooled”。在您的示例中,只有第一个连接具有“1 - Nonpooled”,接下来的 2 个审核登录具有 EventSubClass =“2 - Pooled”。

关于c# - SqlConnection 池似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33309502/

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