最大连接池上限为 100

Sea*_*ong 34 sql-server

我在 Windows Server 2008 机器上运行 SQL Server 2008 R2 SP1。我有一个从 Visual Studio 2010 运行的 .NET 脚本,它执行以下操作:

  • 到达数据库
  • 做出改变
  • 迭代

它将迭代的总次数为 150,但是它在 100 个连接处停止,我不知道为什么。我可以调整我的脚本以仅使用单个线程,但我更想知道我在哪里缺少最大连接设置,因为这对于将来参考会更有用。

这是我到目前为止检查过的地方:

  • Visual Studio 2010 中的 SQL 连接字符串(设置为 1000)
  • SSMS 数据库实例连接属性(设置为 0 [infinity] 用户连接)
  • google了一些Server 2008的资料,貌似可以处理100多个连接
  • 逐步完成我的代码,SP_WHO2其中提供了有关逻辑连接的更多信息,看到连接数从 52 开始,脚本错误与“达到最大池连接数”错误在 152 个逻辑连接处。
  • 更改了要使用的连接字符串 Data Source=PerfSQL02;Initial Catalog=Masked;Integrated Security=True;Max Pool Size=1000

我不知道还有哪里可以检查,我知道我这里有很多活动部件,但我感觉我只是在某处缺少最大池设置。

Tho*_*ger 32

在您的连接字符串中,指定max pool size=<your desired max pool size>. 因此,换句话说,如果您想将最大池大小更改为值 500,您的连接字符串可能类似于:

"data source=your_server; initial catalog=your_db; trusted_connection=true; max pool size=500"
Run Code Online (Sandbox Code Playgroud)

显然,我对您的其他参数做了很多假设,但这应该会让您对如何进行有一个好主意。连接池是客户端提供程序强制执行。客户端需要通过连接字符串指定此最大池大小设置。

此外,请确保您正确关闭或处理您的连接,否则您将占用连接。像这样的东西(C#):

string connectionString = "data source=your_server; initial catalog=your_db; trusted_connection=true; max pool size=500";

using (SqlConnection dbConn = new SqlConnection(connectionString))
{
    using (SqlCommand sqlCmd = new SqlCommand())
    {
        sqlCmd.Connection = dbConn;
        sqlCmd.CommandText = "select 1;";

        // ... so on and so forth
    }
}
Run Code Online (Sandbox Code Playgroud)

using块(在 C# 中)IDisposable.Dispose()在完成时调用。您也可以在块的块中实现SqlConnection.Dispose()或。SqlConnection.Close()finallytry/catch/finally

参考: 关于 SqlConnection.ConnectionString 属性的 MSDN 文档

  • 这是您的连接字符串*无处不在*吗?我问的原因是因为不同的连接字符串将是不同的连接池。 (3认同)
  • 这应该被标记为接受的答案,因为它实际上正确地回答了 OP。他提到了客户端连接字符串 Max Pool Size 参数和关闭/处理连接的需要。 (2认同)

Mik*_*Fal 30

默认情况下,SQL Server 最多允许32767 个连接。可以使用sp_configure. 要查看此设置的当前配置,请使用以下查询:

select * from sys.configurations
where name ='user connections'
Run Code Online (Sandbox Code Playgroud)

默认情况下,您应该看到最大值为 32767,value_in_use等于 0(使用默认设置)。如果这已被更改,您可以重新配置 SQL Server 以使用链接中所述的其他值。

您还应该查看实际建立的连接数,因为您的应用程序之外可能有更多活动(或者您的应用程序建立的连接比您想象的要多)。您需要在perfmon 中查看 General Statistics -> Logical Connections或查询中的值sys.dm_os_performance_counters(cntr_value 将显示当前时间点值):

select * from sys.dm_os_performance_counters
where counter_name ='User Connections'
Run Code Online (Sandbox Code Playgroud)