使用 LINQ to SQL 耗尽数据库连接
在开发一个相对简单的 Web 服务时,该服务获取帖子提供的数据并将其记录在数据库表中,我们收到此错误:
捕获异常:远程服务器返回错误:(500) 内部服务器 Er 或者。 堆栈跟踪:在
某些服务器上位于 System.Net.HttpWebRequest.GetResponse(),但在其他服务器上没有。 得到这个的是物理机,其他的是虚拟机,显然物理服务器更强大。
据我们所知,问题在于每次查询后数据库连接都没有释放回池中。 我使用下面的使用模式:
using (VoteDaoDataContext dao = new VoteDaoDataContext())
{
dao.insert_response_and_update_count(answerVal, swid, agent, geo, DateTime.Now, ip);
dao.SubmitChanges();
msg += "Thank you for your vote.";
dao.Dispose();
}
我添加了 dao.Dispose() 调用以确保在方法完成时释放连接,但我不知道是否有必要。
我正确使用这个模式吗? 我还需要做些什么来确保连接正确返回到池中?
谢谢!
In developing a relatively simple web service, that takes the data provided by a post and records it in a database table, we're getting this error:
Exception caught: The remote server returned an error: (500) Internal Server Er
or.
Stack trace: at System.Net.HttpWebRequest.GetResponse()
on some servers, but no others. The ones that are getting this are the physical machines, the others are virtual, and obviously the physical servers are far more powerful.
As far as we can tell, the problem is that the DB connections aren't being released back to the pools after each query. I'm using the using pattern below:
using (VoteDaoDataContext dao = new VoteDaoDataContext())
{
dao.insert_response_and_update_count(answerVal, swid, agent, geo, DateTime.Now, ip);
dao.SubmitChanges();
msg += "Thank you for your vote.";
dao.Dispose();
}
I added the dao.Dispose() call to ensure that connections are released when the method finishes, but I don't know whether or not it's necessary.
Am I using this pattern correctly? Is there something else I need to do to ensure that connections get returned to the pools correctly?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的诊断信息不够好。 HTTP/500 没有足够的细节来真正判断您的理论是否正确。 如果您想解决问题,您将需要在日志记录中捕获完整的堆栈跟踪。 我想你已经在这里下结论了。 不,您不需要在 using{} 块末尾之前使用 Dispose()。 这就是使用的作用。{}。
Your diagnostic information is not good enough. An HTTP/500 isn't enough detail to really tell if your theory is correct. You're going to need to capture a full stack trace in your logging if you want to get to the problem. I think you've jumped to a conclusion here. And no, you do not need that Dispose() before the end of your using{} block. That's what using{} does.
我认为 dispose() 调用是多余的,但我想确定一下。
我们看到连接池在 SQL 日志中饱和(我无法直接查看,我只是一名开发人员,这些东西在产品环境中运行),而我的运维人员说他看到连接超时。 ..一旦超时,服务器就会再次开始运行,直到下一次连接池饱和。
目前我们正在调整连接池设置...我想确定我没有做错任何事情,因为这是我第一次使用 Linq。
谢谢!
I thought that dispose() call was redundant, but I wanted to be sure.
We're seeing the connection pools saturating in the SQL logs (I can't look at the directly, I'm just a developer, and this stuff's running in a prod environment), and my ops guy said he's seeing connections timing out... and once they time out, the server starts running again, until the next time it saturates the connection pool.
We're going through the process of tweaking the connection pool settings at the moment... I wanted to be certain that I wasn't doing anything wrong, since this is my first time using Linq.
Thanks!