已达到错误最大池大小?
我认为这是因为我没有关闭与数据库的连接。我发布了我在下面为我的数据层使用的代码。我需要关闭我的连接吗?我也该怎么做呢?这是导致问题的代码吗?
错误代码如下:
超时已到。从池中获取连接之前超时时间已过。发生这种情况的原因可能是所有池连接都在使用中并且已达到最大池大小。
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.InvalidOperationException:超时已过期。从池中获取连接之前超时时间已过。发生这种情况的原因可能是所有池连接都在使用中并且已达到最大池大小。
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 think this is because im not closing conections to my DB. I posted code im using below for my Datalayer. 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. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: 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.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信 SqlDataAdapter 自己处理连接。然而,在多个背靠背 fill() 到数据适配器的情况下,在每个 fill() 请求中打开连接会提高性能。结果是数据库连接被打开和关闭多次。
我认为你可以自己控制连接。
I believe that SqlDataAdapter handle connection by itself. 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. The result is that database connection being opened and close several times.
I think that you can control the connection by yourself.
填充后添加此行。
编辑:
您还可以在 IIS 中回收池。但最好的做法是在使用后关闭连接。
add this line after fill.
EDIT:
Also you can recycle pool in IIS. but its best practice to close connection once used.
如果您不想猜测是否有效地处理连接,您可以运行查询来了解打开的连接数量:
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:
using 确保将调用 Dispose。我认为发布的代码很好,
默认最大池大小为100。使用集成安全性和登录用户不太可能超过100。
请检查配置文件中DataBaseConnection中的某些设置是否与连接池冲突。请参阅: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.80).aspx
请检查是否SqlDataAdapter 或 SqlDataConnection 对象不会在其他地方进行处理。
using makes sure the Displose will be called. 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.
Please check whether some setting in DataBaseConnection in config file conflict with connection pool. refer to :http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.80).aspx
And please check whether SqlDataAdapter or SqlDataConnection object is not dispose in other places.