OLE DB、ODBC 和 Oracle 连接池OLE DB, ODBC, and Oracle connection pooling

池连接可以显著提高应用程序的性能和可缩放性。Pooling connections can significantly enhance the performance and scalability of your application. 本部分介绍 OLE DB、ODBC 和 Oracle 的 .NET Framework 数据提供程序的连接池。This section discusses connection pooling for the .NET Framework data providers for OLE DB, ODBC, and Oracle.


OLE DB .NET Framework 数据提供程序使用 OLE DB 会话池自动管理连接池。The .NET Framework Data Provider for OLE DB automatically pools connections using OLE DB session pooling. 连接字符串自变量可用于启用或禁用包括池在内的 OLE DB 服务。Connection string arguments can be used to enable or disable OLE DB services including pooling. 例如,以下连接字符串禁用 OLE DB 会话池和自动事务登记。For example, the following connection string disables OLE DB session pooling and automatic transaction enlistment.

Provider=SQLOLEDB;OLE DB Services=-4;Data Source=localhost;Integrated Security=SSPI;

我们建议您在使用完连接后始终将其关闭或释放,以便连接可以返回到池。We recommend that you always close or dispose of a connection when you are finished using it in order to return the connection to the pool. 不是显式关闭的连接可能无法返回池。Connections that are not explicitly closed may not get returned to the pool. 例如,如果连接已超出范围但没有显式关闭,则仅当达到最大池大小而该连接仍然有效时,该连接才会返回到连接池中。For example, a connection that has gone out of scope but that has not been explicitly closed will only be returned to the connection pool if the maximum pool size has been reached and the connection is still valid.

有关 OLE DB 会话或资源池的详细信息,以及如何通过覆盖 OLE DB 提供程序服务默认值来禁用池,请参阅OLE DB 程序员指南For more information about OLE DB session or resource pooling, as well as how to disable pooling by overriding OLE DB provider service defaults, see the OLE DB Programmer's Guide.


ODBC .NET Framework 数据提供程序的连接池由用于该连接的 ODBC 驱动程序管理器管理,不受 ODBC .NET Framework 数据提供程序的影响。Connection pooling for the .NET Framework Data Provider for ODBC is managed by the ODBC Driver Manager that is used for the connection, and is not affected by the .NET Framework Data Provider for ODBC.

若要启用或禁用连接池,请在 "控制面板" 的 "管理工具" 文件夹中打开 " ODBC 数据源管理器"。To enable or disable connection pooling, open ODBC Data Source Administrator in the Administrative Tools folder of Control Panel. "连接池" 选项卡允许您为安装的每个 ODBC 驱动程序指定连接池参数。The Connection Pooling tab allows you to specify connection pooling parameters for each ODBC driver installed. 特定 ODBC 驱动程序的连接池更改将影响使用该 ODBC 驱动程序的所有应用程序。Connection pooling changes for a specific ODBC driver affect all applications that use that ODBC driver.


Oracle .NET Framework 数据提供程序自动为 ADO.NET 客户端应用程序提供连接池。The .NET Framework Data Provider for Oracle provides connection pooling automatically for your ADO.NET client application. 您也可以提供几个连接字符串修饰符,用于控制连接池的行为(请参见本主题后文的“使用连接字符串关键字控制连接池”)。You can also supply several connection string modifiers to control connection pooling behavior (see "Controlling Connection Pooling with Connection String Keywords," later in this topic).

创建和分配池Create and assign pools

当连接打开时,将根据一种精确的匹配算法来创建连接池,该算法会使连接池与连接中的字符串相关联。When a connection is opened, a connection pool is created based on an exact matching algorithm that associates the pool with the connection string in the connection. 每个连接池都与一个不同的连接字符串相关联。Each connection pool is associated with a distinct connection string. 打开新连接时,如果连接字符串并非与现有池完全匹配,将创建一个新池。When a new connection is opened, if the connection string is not an exact match to an existing pool, a new pool is created.

连接池一旦创建,直到活动进程终止时才会被毁坏。Once created, connection pools are not destroyed until the active process ends. 维护不活动的池或空池占用的系统资源非常少。Maintaining inactive or empty pools uses very few system resources.

连接的添加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.

在请求 OracleConnection 对象时,如果存在可用的连接,则将从池中获取该对象。When an OracleConnection object is requested, it is obtained from the pool if a usable connection is available. 要成为可用连接,该连接当前必须未被使用,具有匹配的事务上下文或者不与任何事务上下文相关联,并且具有与服务器的有效链接。To be usable, the connection must currently be unused, have a matching transaction context or not be associated with any transaction context, and have a valid link to the server.

如果已达到最大池大小且不存在可用的连接,则该请求将会排队。If the maximum pool size has been reached and no usable connection is available, the request is queued. 当连接被释放回池中时,连接池管理程序通过重新分配连接来满足这些请求。The connection pooler satisfies these requests by reallocating connections as they are released back into the pool. 连接在关闭或断开时释放回池中。Connections are released back into the pool when they are closed or disposed.

连接的移除Connection Removal

连接池在长时间处于空闲状态时,或者如果池程序检测到与服务器的连接已断开,连接池将从池中删除连接。The connection pooler removes a connection from the pool after it has been idle for an extended period of time or if the pooler detects that the connection with the server has been severed. 只有在尝试与服务器通信之后,才能检测到此情况。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 connection 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.

如果存在一个与已消失的服务器的连接,如果连接池进程尚未检测到断开的连接并将连接标记为无效,可以从池中提取此连接。If a connection exists to a server that has disappeared, this connection can be drawn from the pool if the connection pooler has not detected the severed connection and marked it as invalid. 当发生这种情况时,将生成异常。When this occurs, an exception is generated. 但是,为了将该连接释放回池中,仍必须将其关闭。However, you must still close the connection in order to release it back into the pool.

不要在类的 Close 方法中对 DisposeConnection 或任何其他托管对象调用 DataReaderFinalizeDo not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. 在终结器中,仅释放类直接拥有的非托管资源。In a finalizer, only release unmanaged resources that your class owns directly. 如果类不拥有任何非托管资源,则不要在类定义中包含 Finalize 方法。If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. 有关详细信息,请参阅垃圾回收For more information, see Garbage Collection.

事务支持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. 因此,每个连接池都将细分为不具有关联事务上下文的连接,并分为N个细分,每个连接池都包含与特定事务上下文的连接。Therefore, each connection pool is subdivided into connections with no associated transaction context and into N subdivisions that each contain 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.

用连接字符串关键字控制连接池Control Connection Pooling with Connection String Keywords

ConnectionString 对象的 OracleConnection 属性支持连接字符串键/值对,可以用于调整连接池逻辑的行为。The ConnectionString property of the OracleConnection object supports connection string key/value pairs that can be used to adjust the behavior of the connection pooling logic.

下表描述了可用于调整连接池行为的 ConnectionString 值。The following table describes the ConnectionString values you can use to adjust connection pooling behavior.

NameName 默认值Default 描述Description
Connection Lifetime 00 连接返回到池中后,创建时间将与当前时间进行比较,如果时间跨度(秒)超过 Connection Lifetime 指定的值,该连接将被破坏。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.

如果值为零 (0),则将使池连接具有最大的超时期限。A value of zero (0) will cause pooled connections to have the maximum time-out.
Enlist 'true''true' 当为 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 100100 池中允许的最大连接数。The maximum number of connections allowed in the pool.
Min Pool Size 00 池中维护的最小连接数。The minimum number of connections maintained in the pool.
Pooling 'true''true' 当为 true 时,将从相应的池中取出连接,或者在必要时创建连接并将其添加到相应的池中。When true, the connection is drawn from the appropriate pool, or if necessary, created and added to the appropriate pool.

另请参阅See also