使用LINQ to SQL耗尽数据库连接

时间:2021-11-23 22:56:36

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:

在开发一个相对简单的Web服务时,它将获取帖子提供的数据并将其记录在数据库表中,我们收到此错误:

Exception caught: The remote server returned an error: (500) Internal Server Er or. Stack trace: at System.Net.HttpWebRequest.GetResponse()

捕获异常:远程服务器返回错误:(500)内部服务器Er或。堆栈跟踪:在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.

我添加了dao.Dispose()调用以确保在方法完成时释放连接,但我不知道是否有必要。

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!

2 个解决方案

#1


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.

您的诊断信息不够好。 HTTP / 500不够详细,无法确定您的理论是否正确。如果要解决问题,则需要在日志记录中捕获完整堆栈跟踪。我想你已经在这里得出结论。不,您在使用{}块结束之前不需要Dispose()。这就是使用{}的功能。

#2


I thought that dispose() call was redundant, but I wanted to be sure.

我认为dispose()调用是多余的,但我想确定。

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.

我们看到连接池在SQL日志中饱和(我不能直接看,我只是一个开发人员,而且这些东西在prod环境中运行),我的操作人员说他看到连接超时了。 ..一旦超时,服务器再次开始运行,直到下次连接池饱和为止。

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.

我们现在正在调整连接池设置的过程......我想确定我没有做错任何事,因为这是我第一次使用Linq。

Thanks!

#1


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.

您的诊断信息不够好。 HTTP / 500不够详细,无法确定您的理论是否正确。如果要解决问题,则需要在日志记录中捕获完整堆栈跟踪。我想你已经在这里得出结论。不,您在使用{}块结束之前不需要Dispose()。这就是使用{}的功能。

#2


I thought that dispose() call was redundant, but I wanted to be sure.

我认为dispose()调用是多余的,但我想确定。

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.

我们看到连接池在SQL日志中饱和(我不能直接看,我只是一个开发人员,而且这些东西在prod环境中运行),我的操作人员说他看到连接超时了。 ..一旦超时,服务器再次开始运行,直到下次连接池饱和为止。

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.

我们现在正在调整连接池设置的过程......我想确定我没有做错任何事,因为这是我第一次使用Linq。

Thanks!