首页 > 解决方案 > 100次运行后mysql定时过期?

问题描述

我正在编写一个一次占用 10 个 ip 的代码 - 检查 ping ,如果有 ping ,他正在将 rime 写入 MySQL

但是每次跑步-他跑了100次后就卡住了,为什么?

MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired.  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.
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at PIng_Parallel.Program.Main(String[] args) in C:\Users\Computer\Documents\Visual Studio 2015\Projects\PIng_Parallel\PIng_Parallel\Program.cs:line 48

这是我正在运行的代码(我相信我可以让它变得更好,稍后会)我在这里被问到,有人告诉我如何让代码以更好的方式运行 - 将 Ip 放入杂志和每次“拍”10个IP。如果有人想告诉我我需要做什么 - 不客气“-)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Net;
using System.Net.Sockets;
using MySql.Data.MySqlClient;
using System.IO;



namespace PIng_Parallel
{
    class Program
    {
        static void Main(string[] args)
        {

            List<string> Online = new List<string>();//save the IP that were Online

            List<string> Top3 = new List<string>(); //save just 10 IP every time

            List<string> ListIPs = new List<string>();//save the IP from the sql

            string[] RouterData = new string[14]; //save the data from the router

            List<string> Answer = new List<string>();//save the IP from the sql
            int running = 0;

            string IP;


            string time;
            again:
            Online.Clear();
            ListIPs.Clear();
            running++;
            MySqlConnection con = new MySqlConnection("Server=10.0.0.249;UserID=Home;password=1234567890;database=sample;SslMode=none");

            string sql = "Select IP From sample.table1 where LastOnline <='20/05/2018' OR  LastOnLine='none' ";

            MySqlCommand cmd = new MySqlCommand(sql, con);

            try
            {
                con.Open();

            }

            catch (Exception e)
            {

                Console.WriteLine(e);
                Console.ReadKey();
                Environment.Exit(0);
            }

            MySqlDataReader reader = cmd.ExecuteReader();


            try
            {
                while (reader.Read())
                {

                    IP = reader["IP"].ToString();
                    ListIPs.Add(IP);

                }
                con.Clone();
            }

            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadKey();
            }


            Console.WriteLine("We have - " + ListIPs.Count + " router to check ");
            //*********************************************//
            int Size = 10; //how may sessions in 1 time

            int num = (ListIPs.Count / Size);
            int part = (ListIPs.Count % Size); 
            int RunTime = 0;
            MySqlConnection conn2 = new MySqlConnection("Server=10.0.0.249;UserID=Home;password=1234567890;database=sample;SslMode=none");

            for (int i = 0; i < ListIPs.Count; i++)//want to run the command for only 3 in each time
            {
                RunTime++;


                Top3 = ListIPs.GetRange(i, Size-1);
                System.Threading.Tasks.Parallel.ForEach(Top3, site =>
                {
                    try
                    {
                        Ping p = new Ping();

                        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                        byte[] buffer = Encoding.ASCII.GetBytes(data);
                        int timeout = 200;

                        PingReply reply = p.Send(site, timeout, buffer);
                        if (reply.Status == IPStatus.Success)
                        {
                            time = DateTime.Now.ToString();

                         //   Online.Add(site + "," + reply.Status.ToString() + " , " + reply.RoundtripTime.ToString()+ " - " + time);
                      Online.Add("Update table1  SET LastOnline='" + time + "'" + "Where IP='" + site + "'");

                            //    RouterData = GetData(site);
                        //    foreach (string h in RouterData)
                        //    {
                        //        Answer.Add(h);
                        //    }


                        }

                    }

                    catch (Exception e)
                    {
                        Console.WriteLine(site);
                        Console.WriteLine(e);
                        Console.ReadKey();
                    }

                    finally
                    { }

                });///end of Threading

                // string sql2 = ("Update table1  SET LastOnline='" + time + "'" + "Where IP='" + site + "'");

                try
                {
                    foreach (string sql2 in Online)
                    {
                        MySqlCommand write = new MySqlCommand(sql2, conn2);
                        conn2.Open();
                        write.ExecuteNonQuery(); //something like "send command";
                        conn2.Close();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);

                }
                finally
                { }
                Console.WriteLine(RunTime);
                if (RunTime == num)//the last call
                {
                    i = i + part;
                }

                else
                {
                    i = i +(Size-1);
                }



                Top3.Clear();

            } //end of cutting the list to 10 each time

            Console.WriteLine("Finish");
            foreach (var t in Online) //print just the Online IP
            {

                Console.WriteLine(t);

            }
            Console.WriteLine("the program run for " + running + "times");
            goto again;


         //   Console.ReadKey();


        }

标签: c#mysqldatabasemysql-workbench

解决方案


我试着把

finally 
{
conn.Close();
}

但我仍然可以在 MySQL WorkBanch 连接中看到,每次循环开始时,他都会添加另一个连接并且不会关闭最后一个连接,为什么


推荐阅读