IIS 上而非本地的 SQLConnection 超时

SQLConnection timeout on IIS and not locallly

我在 IIS 8.5 上遇到了奇怪的行为。在我使用端点几次(5-6 次)后出现错误:在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且已达到最大池大小。

我的代码是用 net core 3.1 编写的,连接到 sql 服务器并尝试执行存储过程。它可以在本地处理任意数量的呼叫。在本地验证没有抛出异常。

 public int GetCount()
 {
        int jobCount = 0;
        using (SqlCommand com = new SqlCommand("dbo.GETCount", new SqlConnection(this.ConnectionString)) { CommandType = CommandType.StoredProcedure })
        {
            com.Parameters.Add(new SqlParameter("@jobCount", SqlDbType.Int) { Direction = ParameterDirection.Output });
            try
            {
                com.Connection.Open();
                com.ExecuteNonQuery();
                jobCount = (int)com.Parameters[0].Value;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                com.Connection.Close();
            }

        }

        return jobCount;
    }

有什么建议吗?

这是我的建议。

  1. 始终在 finally 块中关闭连接

  2. 增加连接字符串中的池大小

    string connectionString = "数据源=localhost;初始目录=Northwind;" +

    “集成安全性=SSPI;最小池大小=10;最大池大小=100”;

  3. 根本不使用池

    string connectionString = "数据源=localhost;初始目录=Northwind;" +

    “集成安全性=SSPI;池化=false;”;