连接池超时的问题!!!

lutaotony 2009-09-11 03:49:05
最近不知道为什么老是报数据库连接超时。
错误信息:error connecting: 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 IAsyncResult IAsyncQuery(int id)
{
DataBase db = GetDataBase(DataBaseName);
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
using (DbCommand command = db.GetSqlStringCommand(base["Select语句"]))
{
db.AddInParameter(command, "id", DbType.Int32, id);
return db.ExecuteAsyncDataReader(command, CommandBehavior.CloseConnection);
}
connection.Close();
}
}
恳请高手赐教。
本人新手没多少分,见谅!!
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lutaotony 2009-09-11
  • 打赏
  • 举报
回复
请问应该怎样放呢!
using (DbCommand command = db.GetSqlStringCommand(base["Select语句"]))
{
db.AddInParameter(command, "id", DbType.Int32, id);
connection.Close();
return db.ExecuteAsyncDataReader(command, CommandBehavior.CloseConnection);
}
那这样改可以吗?,为什么using没起到作用呢?
lutaotony 2009-09-11
  • 打赏
  • 举报
回复
那么应该return放在外面吗?
卧_槽 2009-09-11
  • 打赏
  • 举报
回复
using (DbCommand command = db.GetSqlStringCommand(base["Select语句"]))
{
db.AddInParameter(command, "id", DbType.Int32, id);
return db.ExecuteAsyncDataReader(command, CommandBehavior.CloseConnection);
}
connection.Close();
在close之前,已经return了,所以这里连接未关闭
lutaotony 2009-09-11
  • 打赏
  • 举报
回复
为什么我不用异步的没问题呢
public DataTable Query(int id)
{
DataBase db = GetDataBase(DataBaseName);
using (DbCommand command = db.GetSqlStringCommand(base["Select"]))
{
db.AddInParameter(command, "id", DbType.Int32, id);
return db.ExecuteDataSet(command).Tables[0];
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧