Pooling

INTRODUCTION

Terms
Overall Mechanism

The overall process of pooling works as follows: As applications open connections to a data source and subsequently release them, a pool of open connections with complete authentication and connection properties is built. Then if an application requests the same data store with the same authentication and connection properties, the connection will be satisfied from the pool, if available, rather than creating the connection again. Connections are held in the pool for a specific time after the application releases them. They are released by the pooling mechanism when they time out.

Benefits

Main benefit to connection pooling is performance - Establishing a connection to a database can be very expensive depending on:

Pool Contents

In general, all data source connections that share the same exact properties (i.e., connection string in ADO, ODBC properties, and OLE DB properties) are placed in the same pool. If a connection had a different property or even an extra property, it is placed in a different pool.

closing Recordset & Connection objectS

The close method on a Recordset or Connection object releases resource associated with that object from memory. The object is still valid (i.e., still in memory). Setting the object to Nothing destroys the object and removes it from memory

POOLING BENEFITS

Overview

Any application that makes frequent calls to a data source for connections can benefit from pooling

Details

Note the following:

ODBC POOLING

Overview

When using ODBC drivers, pooling proceeds transparent to the application as follows: 

Configuring ODBC pooling

ODBC pooling can be configured in three different ways:

Registry

This is only recommended for pre MDAC 1.5. Use the following registry key to set the request values:

 HKEY_LOCAL_MACHINE/SOFTWARE.ODBC/ODBCINST.INI/

Native ODBC APPLICATION (ODBC API)

All actions performed with the ODBC administrator can be accomplished with an application using ODBC API. Further, pooling can be fine grained from being applicable to a specific driver or to being applicable to a specific environment handle.

Number of pools

With ODBC pooling, 

number of available pools = number of available processors

Irrespective of the number of pools, a single pool can contain pooled connections to different drivers. And for specific driver, the pool can contains pooled connection to different data sources.

Monitoring pooling

Use PerfMon system application to monitor pools and their contents

Database Context

Always maintain the original database context before disconnection: Assume your connection points to the pubs database. In your application you then decide to access the Master database to get some metadata for example, and then disconnect. The database context has changed from pubs to Master and the connection with this context change is returned to the pool. This may cause problems in the pool. See MSDN article Q197459, "PRB: ADO 2.0 Returns Connection Messages to Browser."

OLE DB POOLING

Overview

When using OLE DB providers, pooling proceeds transparent to the application as follows:

Configuring OLE DB pooling

ODBC pooling can be controlled in different ways:

Note on Retry Wait value

This value works differently than in ODBC - If the server is unavailable, the pool is blocked by OLE DB. The next retry for a valid connection occurs after one minute. If this attempt fails, the The next retry occurs after two minutes, and again after five minutes. Thereafter, every five minutes until a valid connection is returned.

Note

Pooling will not be enabled if CoCreateInstance is called on the CLSID of the provider

CoCreateInstance(CLSID_MSDASQL, ...);

Number of Pools

In OLE DB a given pool contains connection for a single provider (say OLE DB For SQL Server) with a specific set of connection attributes. That is, each pool can only contains connections to one set of attributes and one data source.

No Of Pools = (No of Processors + 1) x No of distinct sets of connection attributes.

OLE DB Tips

ADO
OLE DB

TroubleShooting

Connection Creep

An increase in the number of open connections on a database server, i.e., SQL Server. Common causes

Closing Connections
Disconnecting record set objects

Set oRS.ActiveConnection = Nothing    'Disconnect recordset
oCN.close                             'Return connection to pool
set oCN = Nothing                     'Release connection from pool

Temporary Tables

If the connection is used to create some temporary tables (#TableName) for server-side processing, they will not be destroyed until the connection is released from the pool – not just returned to the pool.

Connection Status

The status of any connection can be retrieved with DBPROP_CONNECTIONSTATUS property.

Persistent Connection for Resource Pooling