我如何解决 'Timeout period elapsed prior to obtaining a connection from the pool'?

How do i resolve 'Timeout period elapsed prior to obtaining a connection from the pool'?

我们的生产服务器中不断出现此错误。

The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached

这是我从数据库读取的方式

    objDataEngine.InitiateConnectionAsync();
        SqlParameter[] Parameters = new SqlParameter[3];
        ...
        var execResult = await objDataEngine.ExecuteCommandAsync("WebBrandListSelect", CommandType.StoredProcedure, Parameters);

         while (execResult.Read())
         {
           //read from datareader
         }

         objDataEngine.CloseConnection();

InitiateConnectionAsync 的代码是:

       public async Task InitiateConnectionAsync()
            {

                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = ConfigurationManager.AppSettings[this.database];
                builder.InitialCatalog = this.database;
                builder.UserID = this.username;
                builder.Password = this.password;
                Connection.ConnectionString = builder.ConnectionString;
                await Connection.OpenAsync();
            }

ExecuteCommandAsync

的函数
  public async Task<SqlDataReader> ExecuteCommandAsync(string commandText, CommandType type, SqlParameter[] parameters = null)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 400;
            cmd.Connection = Connection;
            cmd.CommandType = type;
            cmd.CommandText = commandText;
            cmd = AssignParameters(cmd, parameters);
            SqlDataReader Reader =await cmd.ExecuteReaderAsync();
            this.ObjReader = Reader;
            this.ObjSqlCommand = cmd;
            return Reader;
        }

最后是 CloseConnection

的代码
public  void CloseConnection()
{
    this.Connection.Close();
    this.Connection.Dispose();
    if (this.ObjReader!=null)
    {
        this.ObjReader.Close();
        this.ObjReader.Dispose();
        this.ObjReader = null;

    }
    if (this.ObjSqlCommand!=null)
    {
        this.ObjSqlCommand.Dispose();
    }

}

如果您发现我的代码有任何问题,请提供帮助。有没有可能我的数据读取器没有妥善处理?

连接上存在连接超时 属性,这与命令超时无关,因此可能值得尝试设置它。您可以在代码或连接字符串中设置它,如果您使用 SQL/Server.

,它在连接字符串中称为 "Connect Timeout="

我已经快速浏览了您的代码 - 我对处理连接然后处理 reader 和命令感到不安。我认为你应该按此顺序处理命令、reader、连接,尽管我通常不使用处理代码,除非我有理由这样做——垃圾收集器通常在很多情况下都足够好。

如果这没有帮助,我会查看系统配置选项,例如连接池大小等