Connection Pooling

connection

Connection Pooling for the OLE DB .NET Data Provider

This is preliminary documentation and subject to change.
Send feedback on this topic.

The OLE DB .NET Data Provider automatically pools connections using OLE DB session pooling. Connection string arguments can be used to enable or disable OLE DB services including pooling. For example, the following connection string will disable OLE DB session pooling and automatic transaction enlistment.

Provider=SQLOLEDB;OLE DB Services=-4;Data Source=localhost;Integrated Security=SSPI;
For more information about OLE DB session pooling or resource pooling, as well as disabling pooling by overriding OLE DB provider service defaults, see the OLE DB Programmer's Reference in the MSDN library located at http://msdn.microsoft.com/library.

CAUTION   You must always close the Connection when you are finished using it. This can be done using either the Close or Dispose methods of the Connection object. Connections that are not explicitly closed are not added or returned to the pool.

Connection Pooling for the SQL Server .NET Data Provider

This is preliminary documentation and subject to change.
Send feedback on this topic.

The SQL Server .NET Data Provider relies on Windows 2000 Component Services to provide connection pooling using an implicit pooling model by default. The SqlConnection object also has several connection string modifiers that you can set to control implicit pooling behavior.

Pool Creation and Assignment
Each connection pool is associated with one distinct connection string, using an exact matching algorithm. If no exact match is found, a new pool is created.

In the following example, three new SqlConnection objects are created, but only two connection pools are required to manage them. Note that the first and second connection strings differ by Initial Catalog.

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=northwind";
conn.Open();      
// Pool A is created.

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=pubs";
conn.Open();      
// Pool B is created because connection strings differ.

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Integrated Security=SSPI;Initial Catalog=northwind";
conn.Open();      
// Uses pool A.
Once created, connection pools are not destroyed until the active process ends. Maintenance of inactive or empty pools involves minimal system overhead.

Connection Addition
A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size.

When a SqlConnection object is requested, it is obtained from the pool if a usable connection is available. To be usable, the connection must be currently unused, have a matching transaction context or not be associated with any transaction context, and have a valid link to the server.

When the maximum pool size is reached, the request is queued. The object pooler satisfies these requests by reallocating connections as they are released back into the pool. If the time-out period (determined by the Connect Timeout connection string property) elapses before a connection object can be obtained, an error occurs.

CAUTION   You must always close the Connection when you are finished using it. This can be done using either the Close or Dispose methods of the Connection object. Connections that are not explicitly closed are not added or returned to the pool.
Connection Removal
The object pooler will remove a connection from the pool if the connection lifetime has expired, or if the pooler detects that the connection with the server has been severed. Note that this can be detected only after attempting to communicate with the server. If a connection is found that is no longer connected to the server, it is marked as invalid. The object pooler periodically scans connection pools looking for objects that have been released to the pool and are marked as invalid. These connections are then permanently removed.

It is possible for an apparently valid connection to be drawn from the pool although the associated server has disappeared. When this occurs, an exception is generated. However, the user must still close the connection to release it back into the pool.

Transaction Support
Connections are drawn from the pool and assigned based on transaction context. The context of the requesting thread and the assigned connection must match. Therefore, each connection pool is actually subdivided into connections with no transaction context associated with them, and into N subdivisions, each containing connections with a particular transaction context.

When a connection is closed, it is released back into the pool and into the appropriate subdivision based on its transaction context. Therefore, you can close the connection without generating an error, even though a distributed transaction is still pending. This allows you to commit or abort the distributed transaction at a later time.

Controlling Connection Pooling with Connection String Keywords
The ConnectionString property of the SQLConnection object supports some additional connection string key/value pairs that can be used to adjust the behavior of the connection pooling logic.

The following table describes ConnectionString values that you can use to adjust connection-pooling behavior.

Name Default Description
Connection Lifetime 0 When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by connection lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online.
A value of zero (0) will cause pooled connections to never time out.

Connection Reset 'true' Determines whether the database connection is reset when being removed from the pool. Setting to false avoids making an additional server round-trip when obtaining a connection, but you must be aware that the connection state, such as database context, is not being reset.
Enlist 'true' When true, the pooler automatically enlists the connection in the current transaction context of the creation thread if a transaction context exists.
Max Pool Size 100 The maximum number of connections allowed in the pool.
Min Pool Size 0 The minimum number of connections maintained in the pool.
Pooling 'true' When true, the SQLConnection object is drawn from the appropriate pool, or if necessary, created and added to the appropriate pool.
 

时间: 2024-08-04 07:05:28

Connection Pooling的相关文章

Best Practice 6 Use JDBC connection pooling

Best Practice 6 Use JDBC connection pooling <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 为了避免获得.关闭的JDBC connection的高消耗,Websphere Application Server提供基于JDBC2.0的JDBC connection pool(数据连接池).Servlets应用使用Webspher

如何尽快释放掉Connection对象建立的连接?

connection|对象 首先,建立一个Conn的连接对象,然后连接到数据库data.mdb中,取得连接句柄后,把它保存在session("conn")这个Session会话变量中,在打开记录集前,从session("conn")中取出句柄,借助于Session对象,我们可以使不同的ASP页面共用一个连接对象,减少了对服务器内存的开消,而我们也不需要担心当一个用户因为错误操作导致服务器不能释放该被占用的Session连接对象,因为每个用户的Session对象是有一

认识和优化connection对象

connection|对象|优化     在这个数据库连接中.我们使用了session对象,首先,建立一个conn的连接对象,然后连接到数据库data.mdb中,取得连接句柄后,把它保存在session("conn")这个session会话变量中,在打开记录集前,从session("conn")中取出句柄,借助于session对象.我们可以使不同的ASP页面共用一个连接对象,减少了对服务器内存的开销,而我们也不需要担心,当一个客户因为错误操作导致服务器不能析放该被占

OLEDB Resource(Session) Pooling (在Ado开发中使用连接池)

ado|session   Oracle: OLEDB Resource(Session) Poolingby Eric Ma EMa@ompus.jnj.com Database connectivity is a prerequisite for today's dynamically generated web pages. However, connection to databases is one of the most expensive operations one does f

数据库连接池Data Connection Framework

数据库连接池Data Connection Framework具备Connection pooling机制,能管理不同的ADO.net Connection.并通过池对象工厂去PoolObjectFactory去创建数据连接池.(可参考:对象工厂创建--- Singleton Object,SingleCall Object,Pool Object ) 连接池企业应用组件图如下: 类图如下:

J2EE全面介绍

一.J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平台Micro版(Java 2 Platform Micro Edition,J2ME).适用于桌面系统的Java 2平台标准版(Java 2 Platform Standard Edition,J2SE).适用于创建服务器应用程序和服务的Java 2平台企业版(Java 2 Platform Enterprise Edition,J2EE). J2EE是一种利用Java 2平台来简化企业解决方案的开

PHP开发环境搭建详细教程

     首先你需要安装Apache Server,这里以当前最新版Apache Server2.4为例进行说明,可惜官网并没有提供最新版的msi安装包,连编译后的binary二进制压缩包都没提供,不过我已经编译并上传到我的百度网盘,下载地址如下:       Apache Server2.4最新版安装包   下载下来后直接解压到任意目录,如图:  然后你需要安装Apache Server系统服务,先cd 到apacher server2.4/bin下,然后执行如下命令: Java代码   ht

Atomikos多数据源配置项目实例

applicationContext.xml [html] view plain copy  print? <?xml version="1.0" encoding="UTF-8"?>   <beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc

JSP精彩问答集

js <!-- #包括 file="data.inc"-->1.如何混合使用Jsp和SSI #include? 在JSP中可以使用如下方式包含纯HTML: 但是如果data.inc中包含JSP CODE ,我们可以使用:<%@include file="data.inc"%>2.如何执行一个线程安全的JSP? 只需增加如下指令<%@页面 isThreadSafe=& quot ;假的& quot ;%>3.JSP如