원본 참조가 here입니다 :
나는 오라클과 함께 다음과 같은 짓을했는지, 나는 몇 가지 작은 수정 (예를 들어, 등, 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에서,이 같은 섹션을 추가 할 수 있습니다 : 코드에서 당신은 DB 연결을 만들 때마다 다음
<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 DB 연결을위한 래퍼 클래스를 생성하고 위 코드를 연결을 추적 할 수 있도록 배치하는 것입니다. 또는 연결을 만들면 위의 호출을 추가 할 수 있습니다. 래퍼 클래스는 조금 더 깨끗합니다. (우리가 한 일입니다)