title: How do i resolve 'Timeout period elapsed prior to obtaining a connection from the pool'?
body: <p>We keep getting this error in our production server.</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>this is how i read from database</p>
  
  <pre><code>    objDataEngine.InitiateConnectionAsync();
          SqlParameter[] Parameters = new SqlParameter[3];
          ...
          var execResult = await objDataEngine.ExecuteCommandAsync("WebBrandListSelect", CommandType.StoredProcedure, Parameters);
  
           while (execResult.Read())
           {
             //read from datareader
           }
  
           objDataEngine.CloseConnection();
  </code></pre>
  
  <p>code for InitiateConnectionAsync is:</p>
  
  <pre><code>       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();
              }
  </code></pre>
  
  <p>function for <code>ExecuteCommandAsync</code></p>
  
  <pre><code>  public async Task&lt;SqlDataReader&gt; 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;
          }
  </code></pre>
  
  <p>and finally the code for <code>CloseConnection</code></p>
  
  <pre><code>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();
      }
  
  }
  </code></pre>
  
  <p>please help if you find anything wrong in my code. Is there any probability my datareader is not properly disposed?</p>