2015-07-02 31 views
1

您对我将如何管理我的应用程序以正确运行有任何建议吗?它基本上更新我的数据库表中的所有行(203行,但可以更多)。我需要整天运行它。运行一小时后,它会提示一个错误:使用MySQL的C#中的最大池大小错误

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.

我关闭使用conn.Close()我的连接。

我不确定是否增加池大小将是最好的解决方案,因为它会整天运行,并且可能会达到我设置的池大小。

这里是我的代码:

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()); 
     } 

    } 

现在,我使用using()代替conn.Close。我有另一个function这是pingDesktop,它与不同的查询相同。

+0

你可以发布你的代码吗? –

+0

已发布。请仔细阅读我上面的代码。 – Rowel

回答

1

您没有及时关闭

conn.Open(); 

添加conn.Close中的try-catch的finally

finally 
{ 
    conn.Close();     
} 

在连接字符串中添加max pool size

像这样

public const String connectionString = "server=localhost; uid=root; pwd=; database=it_map;max pool size=5;"; 
+0

我是否正确关闭连接? – Rowel

+0

我刚才提到的那个,你打算关闭那个 –

+0

我已经应用了这些修改。 'max pool size = 5'足够整天运行我的应用程序而不需要再次提交MySqlException呢? – Rowel