title: Maximum pool size error in C# using MySQL
body: <p>Do you have any suggestion on how I will manage my application to run properly? It basically updates all rows (203 rows but can be more) in a table of my database. And I need to run it all day. After an hour of running, it prompts an error:</p>
  
  <p><code>MySqlException: error connecting: Time out expired. The timeout period elapsed prior and max pool size was reached to obtaining a connection from the pool. This may have occurred because all pooled connection were in use.</code></p>
  
  <p>I close my connection using <code>conn.Close()</code>. </p>
  
  <p>I'm not sure if increasing the pool size will be the best solution for this since it will run all day and possibly it may reach the pool size I set.</p>
  
  <p>Here's my code: </p>
  
  <pre><code>public static class Globals 
  {
      //Global Variable
      public static String update;
      public static String update2;
      public const String connectionString = "server=localhost; uid=root; pwd=; database=it_map;";
      public static int totalruntime = 0;
  }
  
  static void Main(string[] args) 
  {
      while (true)
      {
          Stopwatch stopWatch = new Stopwatch();
          stopWatch.Start();
  
          Thread t = new Thread(new ThreadStart(pingLaptop));
          Thread t2 = new Thread(new ThreadStart(pingDesktop));
          //Thread t3 = new Thread(new ThreadStart(pingPhone));
          //Thread t4 = new Thread(new ThreadStart(pingLaptop));
  
          Console.WriteLine("\nUpdating all status...\n");
  
          t.Start();
          t2.Start();
          //t3.Start();
          //t4.Start();
  
          t.Join();
          t2.Join();
          //t3.Join();
          //t4.Join();
  
          stopWatch.Stop();
          // Get the elapsed time as a TimeSpan value.
          TimeSpan ts = stopWatch.Elapsed;
  
          // Format and display the TimeSpan value.
          string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
          Console.WriteLine("\nRunTime " + elapsedTime);
  
          Console.WriteLine("\nNext run will start after 1 second...");
          Thread.Sleep(1000);
      }
  }
  
  static void pingLaptop() 
  { 
      String sql = "SELECT * FROM tbl_units WHERE Category=\"Laptop\"";
      MySqlConnection conn, conn2; 
      MySqlCommand command, command2;
      MySqlDataReader reader;
      PingReply reply;
      Ping myPing;
      String netbios_name;
  
      try 
      {
          conn = new MySqlConnection(Globals.connectionString);
          command = new MySqlCommand(sql, conn);
          conn.Open();
  
          reader = command.ExecuteReader();
          while (reader.Read()) 
          {
                  myPing = new Ping();
                  netbios_name = reader.GetString("NetBios_Name");
                  Console.WriteLine("Laptop: " + netbios_name);
                  try {
                      reply = myPing.Send(netbios_name, 2000);
                      if (reply != null) {
                          string status = reply.Status.ToString();
  
                          //Updates the 'Status' of a unit in the database
                          Console.WriteLine(netbios_name + " Status: "+status);
                          if(status.Equals("Success")) {
                              Globals.update = "UPDATE tbl_units SET Status=\"Online\" WHERE NetBios_Name = @name";
                          }
                          else if (status.Equals("TimedOut")) {
                              Globals.update = "UPDATE tbl_units SET Status=\"Offline\" WHERE NetBios_Name= @name";
                          }
  
                          //Builds another connection to database
                          using (conn2 = new MySqlConnection(Globals.connectionString)) {
                              command2 = new MySqlCommand(Globals.update, conn2);
                              command2.Parameters.AddWithValue("@name", netbios_name);
                              conn2.Open();
                              command2.ExecuteNonQuery();                          
                          }
  
                      }
                  }
                  catch (PingException e) {
                      Console.WriteLine("Status: Host is unreachable.");
  
                      Globals.update = "UPDATE tbl_units SET Status=\"X\" WHERE NetBios_Name= @name";
                      using (conn2 = new MySqlConnection(Globals.connectionString)) {
                          command2 = new MySqlCommand(Globals.update, conn2);
                          command2.Parameters.AddWithValue("@name", netbios_name);
                          conn2.Open();
                          command2.ExecuteNonQuery();                      
                      }
                  }
              }
          }
          catch (MySqlException ex)
          {
              Console.WriteLine("Laptop");
              Console.WriteLine(ex.ToString());
          }
  
      }
  </code></pre>
  
  <p>I'm now using <code>using()</code> instead of <code>conn.Close</code>. I have another <code>function</code> which is <code>pingDesktop</code>, it does the same with different query.</p>