gpt4 book ai didi

.net - 如何知道使用 mysql 和 .Net 仍然打开的连接数?

转载 作者:行者123 更新时间:2023-11-30 23:30:23 24 4
gpt4 key购买 nike

我将 Asp.Net 与 MySql 一起使用,我正在尝试解决错误:

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

我正在努力确保我没有泄露任何连接。我需要知道连接池的状态(现在有多少连接打开)?在这个thread我了解到我可以使用性能监视器来了解池计数。但是,当使用 MySql 而不是 Sql Server 时,我该怎么做呢?

最佳答案

下面讨论的上述代码的原始引用是 here :

我已经用 Oracle 完成了以下操作,我认为它可以通过一些小的修改(例如更改 OracleConnection 等)与 MySQL 一起使用。您可以创建一个名为 ConnectionSpy 的类(见下文)。这是在 VB.Net 中,如果您愿意,可以将代码转换为 C#。

Imports System
Imports System.Data
Imports Oracle.DataAccess.Client
Imports System.Diagnostics
Imports System.IO

Public Class ConnectionSpy
Dim con As OracleConnection
Dim st As StackTrace
Dim handler As StateChangeEventHandler
Dim logfileName As String

Public Sub New(ByVal con As OracleConnection, ByVal st As StackTrace, ByVal logfileName As String)
If (logfileName Is Nothing Or logfileName.Trim().Length() = 0) Then
Throw New ArgumentException("The logfileName cannot be null or empty", logfileName)
End If

Me.logfileName = logfileName
Me.st = st
'latch on to the connection
Me.con = con
handler = AddressOf StateChange
AddHandler con.StateChange, handler
'substitute the spy's finalizer for the
'connection's
GC.SuppressFinalize(con)
End Sub
Public Sub StateChange(ByVal sender As Object, ByVal args As System.Data.StateChangeEventArgs)
If args.CurrentState = ConnectionState.Closed Then
'detach the spy object and let it float away into space
'if the connection and the spy are already in the FReachable queue
'GC.SuppressFinalize doesn't do anyting.
GC.SuppressFinalize(Me)
RemoveHandler con.StateChange, handler
con = Nothing
st = Nothing
End If
End Sub
Protected Overrides Sub Finalize()
'if we got here then the connection was not closed.
Using stream As FileStream = New FileStream( _
logfileName, _
FileMode.Append, _
FileAccess.Write, _
FileShare.Write)

Using sw As StreamWriter = New StreamWriter(stream)
Dim text As String = _
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & _
": WARNING: Open Connection is being Garbage Collected" & _
Environment.NewLine & "The connection was initially opened " & st.ToString()
sw.WriteLine(text)
sw.Flush()
RemoveHandler con.StateChange, handler
'clean up the connection
con.Dispose()
End Using

End Using
End Sub
End Class

然后,在您的 web.config/app.config 中,您可以添加如下部分:

<configuration>
<appSettings>
<!--
if you need to track connections that are not being closed, set
UseConnectionSpy to "true". If you use the connection spy, a log
will be created (the logfile can be specified with the ConnectionSpyLog
parameter) which will give you a stack trace of each code location
where a connection was not closed
-->
<add key="UseConnectionSpy" value="true"/>
<add key="ConnectionSpyLog" value="c:\\log\\connectionspy.log"/>
</appSettings>
</configuration>

然后,无论您在代码中的何处创建数据库连接,都可以执行此操作(见下文)。mConnection 变量将代表您的 MySQL 连接,当您到达这点。

    If ConfigurationManager.AppSettings("UseConnectionSpy") = "true" Then
Dim st As StackTrace = New StackTrace(True)
Dim sl As ConnectionSpy = New ConnectionSpy(mConnection, st, _
ConfigurationManager.AppSettings("ConnectionSpyLog"))
End If

那么,如果您有任何连接在关闭之前被垃圾收集,它将在日志文件中报告。我已经在我的一些应用程序中使用了这种技术并且效果很好。

您需要做的是为您的 MySQL 数据库连接创建一个包装器类,您可以将上面的代码放在适当的位置来跟踪连接。或者,您可以将上述调用添加到您创建连接的任何位置。包装类虽然更干净一些(我们就是这样做的)

关于.net - 如何知道使用 mysql 和 .Net 仍然打开的连接数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com