繁体   English   中英

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

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

在加载网站上的一个页面时,似乎经常出现以下错误。

“超时已过期。在从池中获取连接之前已经过了超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。”

根据我从搜索中发现的内容,似乎数据库连接没有正确关闭并因此泄漏。 但我对 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();

    }

您的问题可能是由于缺少连接 object 造成的。 应释放一次性object (IE 调用其 Dispose 方法)以释放 object 中包含的所有非托管资源。 using 语句将使这变得简单,因为即使在出现异常的情况下,它也会为包含在其起始块中的 object 调用 Dispose 方法。

using 语句是编写 try/finally 块的简单方法

SqlConnection con = new SqlConnection();
try
{

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

因此,您的代码可以更改为:

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

当然,要消除连接池问题,您应该检查创建 SqlConnection 并实现 using 块的所有代码。

暂无
暂无

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

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