开发者

memory leak / badly written code or both - properly disposing of sqlconnection?

I am investigating an issue with a .net 1.1 web application where it seems that we had quite a few "Internal connection fatal error" exceptions possibly the connection objects staying open or not disposed of properly. The web server eventually crashes under heavy load. I checked the code and we are in fact calling sqlconnection.close() in all places in the try catch finally

The project implements the following pattern. Can someone tell me if it looks like this may cause a memory leak?

The aspx webpage makes the following call in Private Sub Page_Load

Dim oDictionary As New dbDictionary
tagVal = oDictionary.GetTag(41)

where dbDictionary is used for getting a sql table from DB

Public Class dbDictionary
    Inherits DBInteractionBase

Public Function GetTag(ByVal tagId)

       'uses the  _connection connection object and Executes sql which sometimes throws a sql exception, but closes the connection in finally

        GetDictConnectionString()

         'new sqlconnection object for another sqlcommand
        Dim dictConnection As New SqlConnection
        dictConnection = _connection  'At this point the _connection is closed.

        'I think the dictConnection is a reference to _connection

        Dim cmdToExecute As SqlCommand = New SqlCommand

        ' // Use base class' connection object
        cmdToExecute.Connection = dictConnection

        Try
            ' // Open connection.
            dictConnection.Open()

            ' // Execute query.
            adapter.Fill(toReturn)


        Catch ex As Exception
            Throw New Exception("Error occured.", ex)
        Finally
            ' // Close connection.
            dictConnection.Close()
            cmdToExecute.Dispose()
            adapter.Dispose()
        End Try


    End Function

    Private Function GetDictConnectionString()


        ' initialize SqlCommand... Use base class _connection object
        cmdToExecute.Connection = _connection

        Try
            _connection.Open()
            adapter.Fill(toReturn)

        Catch ex As Exception
            Return "Error" ' Could this be the issue?  The original exception isn't released?
        Finally
            _connection.Close()
            cmdToExecute.Dispose()
            adapter.Dispose()
        End Try

End Class

Here is the DBInteractionBase that it in开发者_如何学运维herits

Public MustInherit Class DBInteractionBase
    Inherits System.Web.UI.Page

Protected _connection As SqlConnection

    Public Sub New()

         _connection = New SqlConnection
         _connection.ConnectionString = "some conn string"
            End Sub

    Public Overloads Sub Dispose()
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub


    Protected Overridable Overloads Sub Dispose(ByVal isDisposing As Boolean)
        ' // Check to see if Dispose has already been called.
        If Not _isDisposed Then
            If isDisposing Then
                ' // Dispose managed resources.
                _connection.Dispose()
                _connection = Nothing
            End If
        End If
        _isDisposed = True
    End Sub

Now when the code is executing, the Dispose never gets called by the calling web page. What I am wondering is if the dispose code ever gets executed by the GC? Another issue I see possibly is that if GetDictConnectionString has an exception, it never rethrows the original sql exception. Would that somehow leave the sql object in memory? Keep in mind this is a .NET 1.1 application and (GC in .NET 1 is not very efficient)

Also I am wondering what I can monitor on the web server using perfmon to indicate a memory leak. I am planning on modifying this code and would like an indicator that the issue was fixed. I see a trend in SqlClient: Current # connection pools - it's growing steadily 1000 each day (It's Current number of pools associated with the process.) so I am wondering if it should go down as the Sessions decrease. I am looking at (\ASP.NET Apps v1.1.4322(Total)\Sessions Active) to see what the server load looks like.


This code:

'new sqlconnection object for another sqlcommand
Dim dictConnection As New SqlConnection
dictConnection = _connection  'At this point the _connection is closed.

Is creating two SqlConnections (the As New SqlConnection is creating one), and the first would never be disposed. This could certainly be a source of leaking connections.

You're also disposing of the connection _connection that you never created (can't see where this is being created). Assuming it's being created 1:1 this shouldn't be a huge problem, but if an exception occurs before your Try block, this connection will also not be disposed. If _connection is not being created 1:1 for this code, you might end up getting ObjectDisposedException if something else tries to use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜