Bewerken

Share via


OleDbConnection.ReleaseObjectPool Method

Definition

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 ()

Examples

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

Remarks

ReleaseObjectPool can be called to free resources that would otherwise be reserved for pooled OleDbConnection objects. 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. Call Close to return the connection object to the pool.

  2. Allow each connection object to time out of the pool.

  3. Call ReleaseObjectPool.

  4. Invoke garbage collection.

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.

Applies to

See also