Export (0) Print
Expand All

OleDbConnection.ReleaseObjectPool Method

Indicates that the OleDbConnection object pool can be released when the last underlying connection is released.

Namespace: System.Data.OleDb
Assembly: System.Data (in system.data.dll)

public static void ReleaseObjectPool ()
public static void ReleaseObjectPool ()
public static function ReleaseObjectPool ()

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.

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.
    

using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
        string x = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=AdventureWorks;"
            + "Integrated Security=SSPI";
        OpenConnection(x);
        Console.ReadLine();
    

    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.
        

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft