简体   繁体   English

如何修复“从池中获取连接之前已过超时时间”

[英]How to fix “Timeout period elapsed prior to obtaining a connection from the pool”

The following error seems to show every so often when loading up one of the pages on the site.在加载网站上的一个页面时,似乎经常出现以下错误。

"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." “超时已过期。在从池中获取连接之前已经过了超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。”

Based on what I found from searching around, it seems that the database connection isn't being closed properly and hence leaking.根据我从搜索中发现的内容,似乎数据库连接没有正确关闭并因此泄漏。 But I'm not too familiar with ASP.net, so would truly appreciate some guidance.但我对 ASP.net 不太熟悉,所以非常感谢一些指导。

private void DisplayData()
    {
        SqlConnection objCon = new SqlConnection();
        objCon.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        objCon.Open();

        SqlCommand objCmd = new SqlCommand();
        objCmd.Connection = objCon;

        if (Request["Category"] == "some_cat")
        {
            objCmd.CommandText = "Select * From Community Where Category = N'some_cat' Order By Num Desc";
        }
        else if (Request["Category"] == "other_cat")
        {
            objCmd.CommandText = "Select * From Community Where Category = N'other_cat' Order By Num Desc";
        }
        else if (Request["Category"] == "Blog")
        {
            objCmd.CommandText = "Select * From Community Where Category = N'Blog' Order By Num Desc";
        }
        else if (Request["Category"] == "All")
        {
            objCmd.CommandText = "Select * From Community Where Category != N'Blog' Order By Num Desc";
        }


        objCmd.CommandType = CommandType.Text;

        SqlDataAdapter objDa = new SqlDataAdapter();
        objDa.SelectCommand = objCmd;

        DataSet objDs = new DataSet();

        objDa.Fill(objDs, "Community");

        this.ctlList.DataSource = objDs.Tables[0];
        this.ctlList.DataBind();

    objCon.Close();

    }

Your problem is possibly originated by the missing dispose of the connection object.您的问题可能是由于缺少连接 object 造成的。 A disposable object should be disposed (IE calling its Dispose method) to release all the unmanaged resources contained in the object.应释放一次性object (IE 调用其 Dispose 方法)以释放 object 中包含的所有非托管资源。 The using statement will make this easy because, even in case of exceptions, it calls the Dispose method for the object enclsed in its starting block. using 语句将使这变得简单,因为即使在出现异常的情况下,它也会为包含在其起始块中的 object 调用 Dispose 方法。

A using statement is a simple way to write a try/finally block using 语句是编写 try/finally 块的简单方法

SqlConnection con = new SqlConnection();
try
{

}
finally
{
    con.Dispose(); // And this will also close the connection
}

So your code could be changed to:因此,您的代码可以更改为:

private void DisplayData()
{
    using(SqlConnection objCon = new SqlConnection())
    using(SqlCommand objCmd = new SqlCommand())
    {
         objCon.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        objCon.Open();

        objCmd.Connection = objCon;
        objCmd.CommandText = "Select * From Community Where Category = @cat Order By Num Desc";
        objCmd.Parameters.Add("@cat", SqlDbType.NVarChar).Value = Request["Category"].ToString();

        if (Request["Category"] == "All")
        {
           objCmd.CommandText = "Select * From Community Where Category != @cat Order By Num";
           objCmd.Parameters[0].Value = "Blog"    
        }
        SqlDataAdapter objDa = new SqlDataAdapter();
        objDa.SelectCommand = objCmd;
        DataSet objDs = new DataSet();
        objDa.Fill(objDs, "Community");
        this.ctlList.DataSource = objDs.Tables[0];
        this.ctlList.DataBind();
}

Of course, to remove the connection pool problem you should check all your code where you create an SqlConnection and implement the using block.当然,要消除连接池问题,您应该检查创建 SqlConnection 并实现 using 块的所有代码。

暂无
暂无

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

相关问题 ASP - 从池中获取连接之前经过的超时时间 - ASP - the timeout period elapsed prior to obtaining a connection from the pool 从池中获取连接之前经过的超时时间 - The timeout period elapsed prior to obtaining a connection from pool 从池中获取连接之前经过的超时时间 - The timeout period elapsed prior to obtaining a connection from the pool MySqlException:从池中获取连接之前经过了超时时间 - MySqlException: The timeout period elapsed prior to obtaining a connection from the pool SqlConnection-从池中获取连接之前经过的超时时间 - SqlConnection - The timeout period elapsed prior to obtaining a connection from the pool C# 连接池问题:从池中获取连接之前超时时间已过 - C# Connection pool issue: The timeout period elapsed prior to obtaining a connection from the pool 连接泄漏是否会导致Timeout过期。 从池中获取连接之前是否已经过了超时时间? - Will connection leak might Cause Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool? 获取异常超时已过期。 从池中获取连接之前经过的超时时间 - Getting exception Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool 超时已过。 在从池中获取连接之前超时时间已过。 - Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. 让超时过期。 从池中获取连接之前经过的超时时间。 。例外 - Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. .exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM