2012-06-13 6 views
1

나는 MySQL과 함께 Asp.Net를 사용하고 있는데 나는 오류를 해결하기 위해 노력하고있어 : 내가 어떤 연결을 누출하고 있지 않다 있는지 확인하기 위해 노력하고있어mysql과 .Net을 사용하여 아직 열려있는 연결 수를 확인하는 방법은 무엇입니까?

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에서 성능 모니터를 사용하여 풀 수를 알 수 있음을 알게되었습니다. 하지만 SQL Server 대신 MySql을 사용할 때 어떻게 할 수 있습니까? 아래에 설명 위의 코드에 대한

답변

0

원본 참조가 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 연결을위한 래퍼 클래스를 생성하고 위 코드를 연결을 추적 할 수 있도록 배치하는 것입니다. 또는 연결을 만들면 위의 호출을 추가 할 수 있습니다. 래퍼 클래스는 조금 더 깨끗합니다. (우리가 한 일입니다)

관련 문제