override Sub finalize以确保关闭与DataBase的连接

时间:2013-02-12 16:45:31

标签: sql database vb.net visual-studio-2010

我正在连接到Access数据库,我想知道在我的cconnectionDB.vb中覆盖Sub finalize是否有用:

Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.IO

Public Class DbConn
    Private DataCon As DbConnection
    Private DBConnectionOpen As Boolean = False

     Protected Overrides Sub finalize()
            Try
                DataCon.Close()
            Catch ex As Exception

            End Try
        End Sub

    Public Sub OpenDatabaseConnection()
        Try
            DataCon.Open()
        Catch ex As Exception
            Throw New System.Exception("Error opening data connection.", ex.InnerException)
            DBConnectionOpen = False
            Exit Sub
        End Try
        DBConnectionOpen = True
    End Sub 

    Public Sub CloseDatabaseConnection()
        DataCon.Close()
        DBConnectionOpen = False
    End Sub


   ''' <summary>
    ''' Creates a new connection to an Access database
    ''' </summary>
    ''' <param name="FileName">The full path of the Access file</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal FileName As String)
        'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
        Dim fileData As FileInfo = My.Computer.FileSystem.GetFileInfo(FileName)
        DataCon = New OleDb.OleDbConnection
        Dim csb As OleDb.OleDbConnectionStringBuilder = New OleDb.OleDbConnectionStringBuilder
        csb.ConnectionString = "Data Source=" & FileName
        Select Case fileData.Extension.ToLower
            Case ".mdb" : csb.Add("Provider", "Microsoft.Jet.Oledb.4.0")
            Case ".accdb" : csb.Add("Provider", "Microsoft.ACE.OLEDB.12.0")
        End Select
        DataCon.ConnectionString = csb.ConnectionString
        Try
            DataCon.Open()
            DataCon.Close()
        Catch ex As Exception
            Throw New System.Exception("Unable to connect to database.", ex.InnerException)
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

这不是非常有用。在垃圾回收器破坏对象之前,不会调用Finalize解构函数。并且,由于DbConn对象是唯一具有DbConnection对象引用的对象,因此无论如何它都会自动销毁它。如果要在完成连接后立即释放连接,建议的方法是在类中实现IDisposable接口。例如:

Private Class DbConn
    Implements IDisposable

    Private DataCon As DbConnection
    Private disposedValue As Boolean

    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                Try
                    DataCon.Close()
                Catch ex As Exception
                End Try
            End If
        End If
        disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
End Class

但是,如果您实施IDisposable,那么您的工作就不会完成。 Dispose对象上的IDisposable方法永远不会被自动调用。通过手动调用Dispose方法,您需要在完成对象时告诉对象。或者,您可以使用Using块:

Using d As New DbConn
    ' The DbConn.Dispose method automatically gets called once execution leaves this Using block
End Using