简体   繁体   English

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

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

I have an issue with my ASP.NET WebApp.我的 ASP.NET WebApp 有问题。 I store and retrieve my data from MySQL, but the thing is, my data is very huge, resulting in getting this error message:我从 MySQL 存储和检索我的数据,但问题是,我的数据非常庞大,导致收到此错误消息:

MySql.Data.MySqlClient.MySqlException: 'error connecting: Timeout expired. MySql.Data.MySqlClient.MySqlException: '连接错误:超时已过期。 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.'这可能是因为所有池连接都在使用中并且达到了最大池大小。

after I run my program and shuffle between the different tabs which opens a new connection and executes an SQL query.在我运行我的程序并在打开新连接并执行 SQL 查询的不同选项卡之间随机播放之后。

I have searched online to find a way to clear the connection, but the issue is I have to return a MySqlDataReader, which requires me to keep the connection pool open, not allowing me to close the connection pool after retrieving the data, a sample code is shown below:我在网上搜索了一种清除连接的方法,但问题是我必须返回一个MySqlDataReader,这需要我保持连接池打开,不允许我在检索数据后关闭连接池,示例代码如下所示:

        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();
        }

I have tried some solutions, for example, changing it so that when it opens the connection, it checks for any error and if there is, I close it before opening it again:我尝试了一些解决方案,例如,更改它以便在打开连接时检查是否有任何错误,如果有,我会在再次打开它之前将其关闭:

        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();
        }

This does not work, so I try catching the error, and close the connection before opening it again:这不起作用,所以我尝试捕获错误,并在再次打开之前关闭连接:

        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();
        }

But this still produces the same error, where it skips the catch block and goes to conn.Open.但这仍然会产生相同的错误,它会跳过 catch 块并转到 conn.Open。

To give more detail on what I do with the MySqlDataReader, I take the Data and pass the information and binds it into a GridView, displaying the data, hence I cannot close the connection before returning MySqlDataReader:为了更详细地说明我对 MySqlDataReader 所做的操作,我获取数据并传递信息并将其绑定到 GridView,显示数据,因此我无法在返回 MySqlDataReader 之前关闭连接:

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

Any idea on how to solve this issue?关于如何解决这个问题的任何想法? Trying to publish this WebApp onto a server, but if you navigate through the tabs too much, which executes multiple SQL queries, it hangs the whole website, causing it to crash.尝试将此 WebApp 发布到服务器上,但如果您在选项卡中导航过多,这将执行多个 SQL 查询,它会挂起整个网站,导致其崩溃。

Any help is appreciated so do let me know if you need anymore information, thank you in advance!感谢您提供任何帮助,所以如果您需要更多信息,请告诉我,在此先感谢您!

The problem is that the unmanaged resources (the connection) are left open in the method call, and never closed.问题是非托管资源(连接)在方法调用中保持打开状态,并且永远不会关闭。

// 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;
}

And to call the method you should并调用你应该的方法

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

Note : If you were using SQL Server, I know it is not the case, you could have taken benefit of the SqlCommand.ExecuteReader(CommandBehavior) and the CommandBehavior.CloseConnection .注意:如果您使用的是 SQL 服务器,我知道情况并非如此,您可以利用SqlCommand.ExecuteReader(CommandBehavior)CommandBehavior.CloseConnection Sadly it is not the case.可悲的是,事实并非如此。

Move the ** MySqlDataReader ** into the try block将 ** MySqlDataReader ** 移动到 try 块中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM