我有一个流量相当大的 ASP.NET 1.1 网站。我在这个网站项目中有一个类,它处理我所有的数据库访问,称为 DBWrapper:
Public Class dbWrapper
Implements IDisposable
' the private _connVSC2000 property is used for all methods that return scalar and DataType types
Private _connVSC2000 As SqlConnection
Private _connectionString As String
Private _disposed As Boolean = False
Sub New(ByVal connectionString As String)
_connectionString = connectionString
_connVSC2000 = New SqlConnection(connectionString)
_connVSC2000.Open()
End Sub
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Not _disposed Then
'If _connVSC2000.State = ConnectionState.Open Then _connVSC2000.Close()
If disposing Then
_connVSC2000.Dispose()
End If
End If
_disposed = True
End Sub
Protected Overrides Sub finalize()
Dispose(False)
MyBase.Finalize()
End Sub
' Actual database access functions removed as they are not relevant to the question.
end class
我还有一个类(实际上是几个),其中包含许多使用共享 dbWrapper 对象的共享函数:
Public Class SomeClass
Private Shared dbWrapper As New dbWrapper(ConfigurationSettings.AppSettings("VSCConnectionString"))
public shared Function SomeFunction()
'function that utilizes the shared dbWrapper object.
end Function
End Class
我遇到的问题是,一段时间后,连接池会填满,因为连接没有及时释放。我的问题有三个:
dbWrapper 类中的 SQLConnection 对象的处理是否正确实现?
在“SomeClass”中实现 dbWrapper 对象的处置以确保 dbWrapper 对象以及 SQLConnection 对象得到正确处置并且不会阻塞连接池的正确方法是什么?完成后是否需要显式调用 SomeClass.Dispose,还是会在超出范围时自动处理?