OleDbConnection.ReleaseObjectPool 方法

定义

指示可在释放最后一个基础连接时释放 OleDbConnection 对象池。Indicates that the OleDbConnection object pool can be released when the last underlying connection is released.

public:
 static void ReleaseObjectPool();
public static void ReleaseObjectPool ();
static member ReleaseObjectPool : unit -> unit
Public Shared Sub ReleaseObjectPool ()

示例

下面的示例创建一个 OleDbConnection,打开它,显示其部分属性,关闭连接,然后释放对象池以节省资源。The following example creates an OleDbConnection, opens it, displays some of its properties, closes the connection, and releases the object pool to conserve resources.

static void OpenConnection(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("Connection.State: {0}", connection.State);

            connection.Close();
            OleDbConnection.ReleaseObjectPool();
            Console.WriteLine("Connection.State: {0}", connection.State);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}
Public Sub OpenConnection(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Try
            connection.Open()
            Console.WriteLine("Connection.State: {0}", _
                connection.State)

            connection.Close()
            OleDbConnection.ReleaseObjectPool()
            Console.WriteLine("Connection.State: {0}", _
                connection.State)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Using
End Sub

注解

可以调用 ReleaseObjectPool,释放将为池中的 OleDbConnection 对象预留的资源。ReleaseObjectPool can be called to free resources that would otherwise be reserved for pooled OleDbConnection objects. 例如,你可能想要调用此方法,例如,在 OLE DB 服务通常会将共用连接保持活动状态的时间量不会再次使用连接对象。You might want to call this method if, for example, the connection object will not be used again for the amount of time that OLE DB services ordinarily keeps pooled connections active. 请注意,单独调用方法并不会实际释放池中存在的活动连接。Note that calling the method alone does not actually release the active connections that exist in the pool.

在最终释放池之前,必须执行以下操作:The following must occur before the pool is finally disposed:

  1. 调用 Close 将连接对象返回到池。Call Close to return the connection object to the pool.

  2. 允许每个连接对象从池超时。Allow each connection object to time out of the pool.

  3. 调用 ReleaseObjectPoolCall ReleaseObjectPool.

  4. 调用垃圾回收。Invoke garbage collection.

相反,如果对所有活动的连接调用 Close,并调用垃圾回收,但不调用 ReleaseObjectPool,则为共用对象保留的资源仍可用。Conversely, if you call Close on all active connections, and invoke garbage collection, but do not call ReleaseObjectPool, the resources reserved for the pooled objects will remain available.

适用于

另请参阅