title: C# MySQL connection pool limits, and cleaning up connections
body: <p>I have a simple DB manager class (a grander name than it's abilities deserve):</p>
  
  <pre><code>class DbManager
  {
      private MySqlConnectionStringBuilder _connectionString;
  
      public DbManager()
      {
          _connectionString = new MySqlConnectionStringBuilder();
          _connectionString.UserID = Properties.Database.Default.Username;
          _connectionString.Password = Properties.Database.Default.Password;
          _connectionString.Server = Properties.Database.Default.Server;
          _connectionString.Database = Properties.Database.Default.Schema;
          _connectionString.MaximumPoolSize = 5;
      }
  
  
      public MySqlConnection GetConnection()
      {
          MySqlConnection con = new MySqlConnection(_connectionString.GetConnectionString(true));
          con.Open();
          return con;
      }
  
  }
  </code></pre>
  
  <p>I then have another class elsewhere that represents records in one of the tables, and I populate it like this:</p>
  
  <pre><code>class Contact
  {
      private void Populate(object contactID)
      {
          using (OleDbConnection con = DbManager.GetConnection())
          {
              string q = "SELECT FirstName, LastName FROM Contacts WHERE ContactID = ?";
  
              using (OleDbCommand cmd = new OleDbCommand(q, con))
              {
                  cmd.Parameters.AddWithValue("?", contactID);
  
                  using (OleDbDataReader reader = cmd.ExecuteReader())
                  {
                      if (reader.HasRows)
                      {
                          reader.Read();
                          this.FirstName = reader.GetString(0);
                          this.LastName = reader.GetString(1);
                          this.Address = new Address();
                          this.Address.Populate(ContactID)
                      }
                  }
              }
          }
      }
  }
  
  
  class Address
  {
      private void Populate(object contactID)
      {
          using (OleDbConnection con = DbManager.GetConnection())
          {
              string q = "SELECT Address1 FROM Addresses WHERE ContactID = ?";
  
              using (OleDbCommand cmd = new OleDbCommand(q, con))
              {
                  cmd.Parameters.AddWithValue("?", contactID);
  
                  using (OleDbDataReader reader = cmd.ExecuteReader())
                  {
                      if (reader.HasRows)
                      {
                          reader.Read();
                          this.Address1 = reader.GetString(0);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Now I thought that all the <code>using</code> statements would ensure that connections are returned to the pool as they're done with, ready for the next use, but I have a loop that creates hundreds of these Contacts and populates them, and it seems the connections are not being freed up.</p>
  
  <p>The connection, the command and the reader are all in their own <code>using</code> statements.</p>