软件工作过程中偶尔出现unable to connect to any of the specified mysql hosts解决方法

软件工作过程中偶尔出现unable to connect to any of the specified mysql hosts
一个学校的售饭系统,学生刷卡时,会从服务器取得卡信息,人员信息,并判断余额,判断当餐是否超过次数限制,然后执行减余额的操作,并记录本次消费记录。
因为消费记录登记每个学生每次刷卡次数,这个表的记录数会比较多,每天4000次,每月近10万次,判断当餐次数是否超出就是统计这个表的。select count(id) as count from payrecord where cardnum='{0}' and paytype={1} and day='{2}' 有点担心

,几个月或一年后,记录达到百万级,这样的查询会怎样。

系统运行了11天,出现了三次这种情况:unable to connect to any of the specified mysql hosts
软件用的是数据库连接池。按提示来说,这个是无法连接数据库造成的,因为之前工作一直正常,难道是mysql服务崩溃工作引起的?软件使用单位较远,我想请大家帮忙分析一下,大概从哪些方面入手判断。我再去看看。

请大家帮帮忙分析下。分肯定再加
目前想到的办法是:把POOL_MAX_SIZE加大到50或100,然后在MySqlConnection newConn = new MySqlConnection(GetConnString());加个try catch
附连接池代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.IO;
using System.Threading;
using MySql.Data.MySqlClient;
using System.Runtime.CompilerServices;
namespace PosServer
{
public class ConnectionPool
{
private Stack<MySqlConnection> pool;
private const int POOL_MAX_SIZE = 20;
private int current_Size = 0;
private string ConnString = "";//连接字符串 


private static ConnectionPool connPool;


private ConnectionPool()
{
if (pool == null)
{
pool = new Stack<MySqlConnection>();
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static ConnectionPool getInstance()
{
if (connPool == null)
{
connPool = new ConnectionPool();
}
return connPool;
}


public MySqlConnection getConnection()
{
MySqlConnection conn;
lock (this)
{
if (pool.Count == 0)
{
if (current_Size < POOL_MAX_SIZE)
{
conn = createConnection();
current_Size++;
//把conn加入到pool 中


pool.Push(conn);
}
else
{
try
{
Monitor.Wait(this);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
conn = (MySqlConnection)pool.Pop();
}
return conn;
}


private string GetConnString()
{
if (ConnString == "")
{
string ip = "127.0.0.1";
string dbName = "iccard";
string userID = "root";
string userPwd = "11111111";


ConnString = "Database=" + dbName +
";Data Source=" + ip +
";User Id=" + userID +
";Password=" + userPwd + ";" +
"pooling=true;CharSet=utf8;port=3306;";
}
return ConnString;
}


public void releaseConnection(MySqlConnection conn)
{
lock (this)
{
pool.Push(conn);
Monitor.Pulse(this);
}
}


private MySqlConnection createConnection()
{
lock (this)
{
MySqlConnection newConn = new MySqlConnection(GetConnString());
newConn.Open();
return newConn;
}
}
}
}

------解决思路----------------------
建议你不要自己管理MySql连接,用户直接释放,需要直接新建,.NET的连接程序内置连接池,它管理得比你好。
------解决思路----------------------
conn及时释放 每次操作数据库完毕 关闭一下conn