这可能是因为所有池连接都在使用中并且达到了最大池大小

This may have occurred because all pooled connections were in use and max pool size was reached

我的 ASP.NET WebApp 有问题。 我从 MySQL 存储和检索我的数据,但问题是,我的数据非常庞大,导致收到此错误消息:

MySql.Data.MySqlClient.MySqlException: '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.'

在我 运行 我的程序并在打开新连接并执行 SQL 查询的不同选项卡之间随机播放之后。

我在网上搜索了一种清除连接的方法,但问题是我必须 return 一个 MySqlDataReader,这需要我保持连接池打开,不允许我关闭连接pool 取回数据后,示例代码如下:

        public MySqlDataReader GetAllMDH2(string div, string classs, string div2, string queryStr, string selectedmonth, string selectedyear)
        {
            MySqlConnection conn = new MySqlConnection(RBconnstr);
            MySqlCommand cmd = new MySqlCommand(queryStr, conn);
            cmd.Parameters.AddWithValue("@class", classs);
            cmd.Parameters.AddWithValue("@div", div);
            cmd.Parameters.AddWithValue("@div2", div2);
            if (selectedmonth == DateTime.Now.ToString("MMMM") && selectedyear == DateTime.Now.ToString("yyyy"))
            {
                cmd.Parameters.AddWithValue("@thismonth", "%" + DateTime.Now.ToString("MMMM").Substring(0, 3) + "%");
            }
            else
            {
                cmd.Parameters.AddWithValue("@thismonth", "%" + selectedmonth.Substring(0,3) + "%");
            }
            conn.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            return reader;
            conn.Close();
        }

我尝试了一些解决方案,例如,更改它以便在打开连接时检查是否有任何错误,如果有,我会在再次打开之前关闭它:

        public MySqlDataReader GetAllMDH2(string div, string classs, string div2, string queryStr, string selectedmonth, string selectedyear)
        {
            MySqlConnection conn = new MySqlConnection(RBconnstr);
            MySqlCommand cmd = new MySqlCommand(queryStr, conn);
            cmd.Parameters.AddWithValue("@class", classs);
            cmd.Parameters.AddWithValue("@div", div);
            cmd.Parameters.AddWithValue("@div2", div2);
            if (selectedmonth == DateTime.Now.ToString("MMMM") && selectedyear == DateTime.Now.ToString("yyyy"))
            {
                cmd.Parameters.AddWithValue("@thismonth", "%" + DateTime.Now.ToString("MMMM").Substring(0, 3) + "%");
            }
            else
            {
                cmd.Parameters.AddWithValue("@thismonth", "%" + selectedmonth.Substring(0,3) + "%");
            }
            try
            {
                conn.Open();
            }
            finally
            {
                conn.Close();
                conn.Open();
            }
            MySqlDataReader reader = cmd.ExecuteReader();
            return reader;
            conn.Close();
        }

这不起作用,所以我尝试捕获错误,并在再次打开之前关闭连接:

        public MySqlDataReader GetAllMDH2(string div, string classs, string div2, string queryStr, string selectedmonth, string selectedyear)
        {
            MySqlConnection conn = new MySqlConnection(RBconnstr);
            MySqlCommand cmd = new MySqlCommand(queryStr, conn);
            cmd.Parameters.AddWithValue("@class", classs);
            cmd.Parameters.AddWithValue("@div", div);
            cmd.Parameters.AddWithValue("@div2", div2);
            if (selectedmonth == DateTime.Now.ToString("MMMM") && selectedyear == DateTime.Now.ToString("yyyy"))
            {
                cmd.Parameters.AddWithValue("@thismonth", "%" + DateTime.Now.ToString("MMMM").Substring(0, 3) + "%");
            }
            else
            {
                cmd.Parameters.AddWithValue("@thismonth", "%" + selectedmonth.Substring(0,3) + "%");
            }
            try
            {
                conn.Open();
            }
            catch (MySqlException)
            {
                conn.Close();
                conn.Open();
            }
            finally
            {
                conn.Close();
                conn.Open();
            }
            MySqlDataReader reader = cmd.ExecuteReader();
            return reader;
            conn.Close();
        }

但这仍然会产生相同的错误,它会跳过 catch 块并转到 conn.Open。

为了更详细地说明我对 MySqlDataReader 所做的操作,我获取数据并传递信息并将其绑定到 GridView 中以显示数据,因此我无法在 returning MySqlDataReader 之前关闭连接:

gv_meetingrooms.DataSource = meet.GetAllMDH2(div, class, div2, querystr, month, year);
gv_meetingrooms.DataBind();

知道如何解决这个问题吗?试图将此 WebApp 发布到服务器上,但如果您在选项卡中导航过多,这会执行多个 SQL 查询,它会挂起整个网站,导致其崩溃。

感谢任何帮助,如果您需要更多信息,请告诉我,提前谢谢!

将 ** MySqlDataReader** 移到 try 块中

try
{
    conn.Open();
    MySqlDataReader reader = cmd.ExecuteReader();
    return reader;
}
catch (MySqlException ex)
{
    logger(ex);
}
finally
{
    conn.Close();
}
return null;

问题是非托管资源(连接)在方法调用中保持打开状态,从未关闭。

// Note that a MySqlConnection is added as a parameter to the function.
public MySqlDataReader GetAllMDH2(string div, string classs, string div2, string queryStr, string selectedmonth, string selectedyear, MySqlConnection conn)
{
    MySqlCommand cmd = new MySqlCommand(queryStr, conn);
    ...
    return reader;
}

并调用您应该

的方法
MySqlConnection conn = new MySqlConnection(RBconnstr);
MySqlDataReader reader = GetAllMDH2(...., conn);
while(reader.Read())
{
    // Use the datareader
}
conn.Close(); // This line will avoid your error message.

注意:如果您使用的是SQL服务器,我知道情况并非如此,您可以利用SqlCommand.ExecuteReader(CommandBehavior) and the CommandBehavior.CloseConnection。遗憾的是,事实并非如此。