使用MySQL的C#中的最大池大小错误

时间:2015-07-02 01:27:08

标签: c# mysql

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

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,它对不同的查询也是如此。

1 个答案:

答案 0 :(得分:2)

你没有关闭

conn.Open();

在try-catch' conn.Close

中添加finally
finally
{
     conn.Close();                
}

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

喜欢这个

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