一个很着急的数据库连接问题,能解决问题就高分送

xxdino 2003-11-26 04:09:03
我用asp.net写了个应用,在连接过程中,因为采用了大量的存储过程,每次在调用存储过程的时候就会有一个连接到SQLServer,当打开某些页面时,在SQLServer中查进程,竟然会同时出现100多个连接。请问用什么方法可以解决。
我试了用Application和Session/Cache都不行,因为在存储过程中还有大量的Dataset Reader冲突。现在非常着急,请大家帮帮忙,帮我解决这个问题,谢谢。
下面是程序段,这样的数据库连接在程序中有很多次:

public DocClassCollection GetAllClassesByGroupID(int userid,int groupid,int startrecord)
{
// return all of the docclasss and their total and daily posts
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);
SqlCommand myCommand = new SqlCommand("up_GetAllClassesByGroupID", myConnection);

// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;

// Add Parameters to SPROC
SqlParameter para1 = new SqlParameter("@userid", SqlDbType.Int);
para1.Value = userid;
myCommand.Parameters.Add(para1);

// Add Parameters to SPROC
SqlParameter para2 = new SqlParameter("@groupid", SqlDbType.Int);
para2.Value = groupid;
myCommand.Parameters.Add(para2);

// Add Parameters to SPROC
SqlParameter para3 = new SqlParameter("@startrecord", SqlDbType.Int);
para3.Value = startrecord;
myCommand.Parameters.Add(para3);

// Add Parameters to SPROC
SqlParameter para4 = new SqlParameter("@pagesize", SqlDbType.Int);
para4.Value = Globals.DefaultPageSize;
myCommand.Parameters.Add(para4);

// Execute the command
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();

// populate a docDocClassCollection object
DocClassCollection docclasses = new DocClassCollection();
DocClass docclass;
while (dr.Read())
{
docclass = PopulateClassFromSqlDataReader(dr);

docclasses.Add(docclass);
}
dr.Close();
myConnection.Close();
myConnection.Dispose();

return docclasses;
}
...全文
42 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gOODiDEA 2003-11-26
  • 打赏
  • 举报
回复
不是说你一调myConnection.Close();它就会马上关的,GC会在一段时间以后去关,不用管他
saucer 2003-11-26
  • 打赏
  • 举报
回复
you might need to consider

1. use one connection string, since pooling is based on the connection string

2. increase Max Pool Size

3. consider to cache mostly readonly data in Application/Cache variables to reduce database activities
xxdino 2003-11-26
  • 打赏
  • 举报
回复
我用的是sql server 2000 enterprise
现在把pooling关掉以后可以暂时稳住问题,但是又没有其他解决方法呢?如果打开pooling的话,连接根本不是重用的,好像每一个连接都是独占的
saucer 2003-11-26
  • 打赏
  • 举报
回复
what version of sql server are you running? 7 or 2000? enterprise and developer edition? what is your connection string?
xxdino 2003-11-26
  • 打赏
  • 举报
回复
程序执行一段时间以后就会出现这样的问题!!!



超时时间已到。在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 超时时间已到。在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小。

源错误:


行 38:
行 39: // Execute the command
行 40: myConnection.Open();
行 41:
行 42: SqlDataReader dr = myCommand.ExecuteReader();


源文件: h:\许旭\经研asp.net\jydata\data\sqldataprovider.cs 行: 40
saucer 2003-11-26
  • 打赏
  • 举报
回复
you have properly closed the reader and connection, so don't worry, since you are using connection pooling, and the "Max Pool Size" in the connection string is 100 by default, you can reduce this number if you want to, or you can disable pooling by setting the "Pooling" in the connection string to false, but that is not recommended, since it will affect the performance

try to monitor the ADO.NET performance, see

http://techrepublic.com.com/5100-6329_11-5034285-2.html

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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