简体   繁体   English

错误达到最大池大小?

[英]Error max pool size was reached?

I think this is because im not closing conections to my DB. 我认为这是因为我没有关闭我的数据库的连接。 I posted code im using below for my Datalayer. 我在下面为我的数据层发布了代码im。 Do i need to close my conection? 我需要关闭我的牙结石吗? How would i do it too? 我也会怎么做? Is this the code causing problems? 这是引起问题的代码吗?

Heres the error code: 这是错误代码:

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. 这可能是因为所有池化连接都在使用中,并且达到了最大池大小。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.InvalidOperationException: Timeout expired. 异常详细信息:System.InvalidOperationException:超时已过期。 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. 这可能是因为所有池化连接都在使用中,并且达到了最大池大小。

public DataTable getPictures()
    {

        //get database connection string from config file
        string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];

        //set up sql
        string StrSql = "SELECT MEMBERS.MemberName, Picture.PicLoc, Picture.PicID, Picture.PicRating FROM Picture INNER JOIN MEMBERS ON Picture.MemberID = MEMBERS.MemberID WHERE (Picture.PicID = @n) AND (Picture.PicAproval = 1) AND (Picture.PicArchive = 0)AND (MEMBERS.MemberSex = 'F')";

        DataTable dt = new DataTable();
        using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString))
        {
            daObj.SelectCommand.Parameters.Add("@n", SqlDbType.Int);
            daObj.SelectCommand.Parameters["@n"].Value = GetItemFromArray();

            //fill data table
            daObj.Fill(dt);
        }
        return dt;
    }

public int GetItemFromArray()
    {
        int myRandomPictureID;
        int[] pictureIDs = new int[GetTotalNumberOfAprovedPictureIds()];


        Random r = new Random();
        int MYrandom = r.Next(0, pictureIDs.Length);

        DLPicture GetPictureIds = new DLPicture();
        DataTable DAallAprovedPictureIds = GetPictureIds.GetPictureIdsIntoArray();

        //Assign Location and Rating to variables
        int i = 0;
        foreach (DataRow row in DAallAprovedPictureIds.Rows)
        {

            pictureIDs[i] = (int)row["PicID"];
            i++;
        }

        myRandomPictureID = pictureIDs[MYrandom];
        return myRandomPictureID;
    }

 public DataTable GetPictureIdsIntoArray()
    {
        string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];

        //set up sql
        string StrSql = " SELECT Picture.PicID FROM MEMBERS INNER JOIN Picture ON MEMBERS.MemberID = Picture.MemberID WHERE (Picture.PicAproval = 1) AND (Picture.PicArchive = 0) AND (MEMBERS.MemberSex ='F')";
        DataTable dt = new DataTable();
        using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString))
        {

            //fill data table
            daObj.Fill(dt);
        }
        return dt;

    }

I believe that SqlDataAdapter handle connection by itself. 我相信SqlDataAdapter本身可以处理连接。 However, in the case of multiple back-to-back fill() to data adapter, that is more performance to open the connection in each fill() request. 但是,在对数据适配器有多个背对背的fill()的情况下,在每个fill()请求中打开连接的性能更高。 The result is that database connection being opened and close several times. 结果是数据库连接被打开和关闭多次。

I think that you can control the connection by yourself. 我认为您可以自己控制连接。

using (SqlConnection cnn= new SqlConnection (strConectionString))
using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, cnn))
    {
        daObj.SelectCommand.Parameters.Add("@n", SqlDbType.Int);
        daObj.SelectCommand.Parameters["@n"].Value = GetItemFromArray();

        cnn.Open();

        //fill data table
        daObj.Fill(dt);

        cnn.Close();
    }

If you don't want to guess whether you are handling the connections efficiently, you can run a query to tell how many are open: 如果您不想猜测自己是否在有效地处理连接,可以运行查询以了解有多少打开:

SELECT 
    DB_NAME(dbid) as DatabaseName, 
    COUNT(dbid) as ConnectionCount,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
    dbid > 0
GROUP BY 
    dbid, loginame
order by count(dbid) desc

add this line after fill. 填充后添加此行。

daObj.Dispose();

EDIT: Also you can recycle pool in IIS. 编辑:也可以在IIS中回收池。 but its best practice to close connection once used. 但最好的做法是在使用后关闭连接。

using makes sure the Displose will be called. 使用可确保将调用Displose。 I think the posted code is fine 我认为发布的代码很好

Default Max Pool Size is 100. It is not likely that using Integrated Security and logged users more than 100. 默认最大池大小为100。使用Integrated Security且登录的用户不可能超过100。

Please check whether some setting in DataBaseConnection in config file conflict with connection pool. 请检查配置文件中DataBaseConnection中的某些设置是否与连接池冲突。 refer to : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.80).aspx 请参阅: http : //msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.80).aspx

And please check whether SqlDataAdapter or SqlDataConnection object is not dispose in other places. 并且请检查SqlDataAdapter或SqlDataConnection对象是否未放置在其他地方。

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

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