如何知道在 Windows 服务中打开了多少个 SQL 连接?
我看到一些错误表明“连接泄漏”。 也就是说,连接未正确关闭并且池即将耗尽。 那么,我该如何对其进行检测,以准确了解在给定时间有多少个处于开放状态?
I'm seeing some errors that would indicate a "connection leak". That is, connections that were not closed properly and the pool is running out. So, how do I go about instrumenting this to see exactly how many are open at a given time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用 .net,则 PerfMon 中有用于 SQL Server 的 .net 数据提供程序。 您可以在那里查看 NumberOfPooledConnections
If you're using .net, there's the .net data provider for SQL server in PerfMon. You can look at NumberOfPooledConnections there
从数据库方面来看,主表中的 sp_who2 存储过程非常适合此操作。 它将向您显示与数据库的连接。 如果您正在寻找更多数据,也可以尝试分析。
sp_who2 stored procedure in the master table is nice for this from a database side. It will show you connections to the database. If you're looking for more data try profiling as well.
实现一个服务,所有连接都通过该服务创建、打开和关闭。 在那里设立一个柜台。 每次打开或关闭连接时都使用日志记录框架进行记录。
Implement a service that all connections are created, opened and closed through. Hold a counter there. Log with your logging framework each time a connection is opened or closed.
您可以使用探查器工具跟踪所有现有的以及打开和关闭的连接
您可以从企业管理器打开探查器
you can use the profiler tool to trace all existing and opening and closing connections
You can open profiler from enterprise manager
如果您使用的是 SQL 2000,则可以检查 SQL 2000 Enterprise Manager:
(http://technet.microsoft.com/en-us/library/cc738560 .aspx)
(来自 Google 搜索:sql 2000 当前活动)
If you're using SQL 2000, you can check in SQL 2000 Enterprise Manager:
(http://technet.microsoft.com/en-us/library/cc738560.aspx)
(From Google search: sql 2000 current activity)
您可以在 SQL Server Management Studio 或查询分析器中运行 sp_who2 来查看所有当前连接。 那就是 SQL Server。 我不确定您使用的是哪种 RDBMS。
另外,请检查您的代码并确保在不再需要连接时立即将其关闭。 对此保持肛门!
You could run sp_who2 in SQL Server Management Studio or Query Analyser to see all of your curent connections. That is SQL Server. I'm not sure which RDBMS that you are using.
Also, look in your code and make sure that you close a connection as soon as you don't need it anymore. Be anal about this!
使用“using”语句确保您的连接始终关闭,并且您将永远不会再遇到此问题:
Use the "using" statement to ensure your connections are always closed and you'll never have this problem again: