mysql timed expried在运行100次之后?

w6mmgewl  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(286)

我是ruunig的一个代码,采取10个ip在一次-检查平,如果有平,他正在写的雾凇到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

这是我正在运行的代码(我确信我可以做得更好,will-ty-later)我被问到这里,有人告诉我如何让代码运行在一个更好的为什么-把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();

        }
jaql4c8m

jaql4c8m1#

我试着把

finally 
{
conn.Close();
}

但我仍然可以在mysql workbanch连接中看到,每次循环开始时,他都会添加另一个连接并获胜;最后一个关不上为什么

相关问题