简体   繁体   English

监视ADO.NET连接打开时间

[英]Monitoring ADO.NET Connection Open Time

We have a 3-tier application with a C# client, a C# WCF web service layer, and a SQL Server database. 我们有一个带有C#客户端,C#WCF Web服务层和SQL Server数据库的3层应用程序。 The web service connects to the database with ADO.NET. 该Web服务使用ADO.NET连接到数据库。 All of our C# code is using the .NET Framework 2.0. 我们所有的C#代码都使用.NET Framework 2.0。

Recently, a customer performed a stress test on our application. 最近,一位客户对我们的应用程序进行了压力测试。 During the test, the web server generated a lot of errors like the following: 在测试过程中,Web服务器生成了很多错误,如下所示:

Could not connect to database for connection string '...'. 无法连接数据库以获取连接字符串'...'。 Timeout expired. 超时时间已到。 The timeout period elapsed prior to completion of the operation or the server is not responding. 在操作完成之前超时或服务器没有响应。

I am aware there are ways to catch connection errors when the connection pool is full and try acquiring a connection outside of the connection pool. 我知道有一些方法可以在连接池已满时捕获连接错误,并尝试在连接池之外获取连接。 We also found several queries that needed to be tuned, but they dot not explain web server connection timeouts. 我们还发现了一些需要调整的查询,但它们不能解释Web服务器连接超时的问题。

We're trying to figure out why the web server was timing out connecting to the database. 我们试图弄清楚为什么Web服务器超时连接数据库。 I have setup the web server to enable all of the ADO.NET performance counters . 我已经设置了Web服务器以启用所有ADO.NET性能计数器 However, I don't see anything related to the times required to connect or connection timeouts or the like. 但是,我看不到与连接所需的时间或连接超时等有关的任何信息。 Ideally, we would be able to graph the connection times in perfmon next to the other ADO.NET counters. 理想情况下,我们可以在其他ADO.NET计数器旁边以perfmon绘制连接时间。

Is there a way to monitor the ADO.NET performance in acquiring connections? 有没有一种方法来监视ADO.NET在获取连接中的性能?

I imagine we could create our own "average connection open time" performance counter by timing attempts to open connections, but I'd rather use something that already exists. 我想我们可以通过定时尝试打开连接来创建自己的“平均连接打开时间”性能计数器,但是我宁愿使用已经存在的东西。

You can use perfmon to get this information. 您可以使用perfmon获取此信息。 You'll need to attach to the User Connections monitor under SQL Server: General Statistics . 您需要在“ SQL Server: General Statistics下附加到“ User Connections监视器。 Here is the blog post I grabbed that from. 这是我从中获取的博客文章 This will let you know at what point in time connections are being left open. 这将使您知道什么时候断开连接。

You will then need to correlate that with application tasks (ie stuff you're doing in the application when you see them continually climb). 然后,您需要将其与应用程序任务相关联(即,当您看到它们不断攀升时,您在应用程序中正在做的事情)。

Once you've done that you're going to want to get those connections inside a using statement if you don't already: 完成后,您将想要在using语句中获取这些连接(如果您尚未这样做的话):

using (SqlConnection cn = new SqlConnection("some connection string"))
{
    cn.Open();
    ...
}

by doing this you won't need to issue a Close and you won't have to worry about them getting disposed of properly. 这样,您就不必发出Close通知,也不必担心它们会被妥善处理。

In short, the performance counter should help you track down the code in the application that's causing the issue, but it won't be down to the line per say, it will take a bit more effort even from there. 简而言之,性能计数器应该可以帮助您跟踪导致问题的应用程序中的代码,但是不会一字不漏,即使从那里开始,它也会花费更多的精力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM