Ado.net 中的连接池如何工作?

发布于 2024-08-24 17:38:07 字数 263 浏览 2 评论 0原文

我正在使用 System.Data.OdbcClient 连接到旧版 RDBMS 系统。我想知道旧的rdbms系统是否支持连接池?假设我使用如下所示的 Connection ,

using (OdbcConnection con = new OdbcConnection("ConnStr"))
{
    con.Open();

    //perform db operation

}

它是在每次调用时建立连接并断开连接还是来自连接池。

I am connecting to a legacy rdbms system using System.Data.OdbcClient. I would like to know if the connection pool is supported by the legacy rdbms system? Suppose I use Connection as shown below

using (OdbcConnection con = new OdbcConnection("ConnStr"))
{
    con.Open();

    //perform db operation

}

Does it establishes a connection and disconnects each time it is called or it comes from connection pool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-08-31 17:38:07

这完全取决于该计算机上连接池的配置方式。如果在此计算机上有效启用连接池,则显示的代码段将使用池中的连接(如果可用),并且实际上不需要创建新的 [RDBMS] 连接。

这就是连接池的好处:它对应用程序是透明的,即您不需要做任何不同的事情,调用单独的 API 等。

有一个区别:连接池通常处理与 DBMS 服务器的连接( “SQL 会话”(如果您愿意)不使用封装此类连接的对象。因此,SQLsessions(相对而言,生成成本最高的元素)被有效地缓存,但 ADO(或任何对象)每次都会重新创建。

连接池还确保与 SQL Server 的连接得到有效使用,但不保证不会创建新连接(例如,在相对空闲一段时间后,某些连接可能会超时,然后被删除并重新连接)。 -创建)。

编辑(关于对旧版 RDBMS 等的支持。[Amitabh 的评论])
对于 ODBC,连接池是 ODBC 层的一项功能,而不是 ODBC 用于连接到底层存储的各种驱动程序的功能。因此,只要您拥有 ODBC 版本 3.0 或更高版本(并且只要 ODBC 可以访问底层驱动程序),ODBC 就可以为您管理连接池(前提是您为其提供必要的配置详细信息)。
使用 ODBC,似乎可以通过编程方式配置/启用连接池。这并不会使连接池对程序透明的说法无效,只是您可能需要在程序的初始化部分中进行一些调用来设置池,保留实际使用连接的其余逻辑不变。
例如,请参阅此MSDN 文章

It all depends on the way connection pooling is configured on this machine. If connection pooling is effectively enabled on this computer, the snippet shown will use a connection from the pool (if available), and effectively not require the creation of a new [RDBMS] connection.

That's the nice thing about connection pooling: it is transparent to the application, i.e. you do not need to do anything different, to call a separate API etc.

There's a distinction to be made: connection pooling typically deals with connections to the DBMS server ("SQL sessions" if you will) not with the object which encapsulate such a connection. Consequently, the SQLsessions (which are, relatively, the most costly elements to produce) are effectively cached, but the ADO (or whatever objects) are created anew each time.

Also connection pooling ensures that connections to the SQL server are use efficiently, but doesn't warranty that a new connection doesn't get created (for example after a period of relative idle, some connections may time out, and are then dropped and re-created).

Edit (on the support for legacy RDBMS etc. [Comment from Amitabh])
With ODBC, connection pooling is a feature of the ODBC layer, not of the various drivers ODBC uses to connect to the underlying storages. Therefore, so long as you have ODBC version 3.0 or later (and so long the underlying driver is accessible to ODBC), ODBC can manage a connection pool for you (provided you supply it the necessary configuration details).
With ODBC, it appears that the connection pool can be configured/enabled programmatically. This doesn't invalidate the statement that connection pooling is transparent to the program, it's just that you may need, in the initialization section of the program, make a few calls to setup the pooling, the remainder of the logic actually using connections being kept unchanged.
see for example this MSDN article

双马尾 2024-08-31 17:38:07

它使用连接池,您可以(应该)将其放在 using 子句中,该子句将“软关闭”连接,但下次您可能会获得已建立的连接。

It uses a connection pool, you can (ans should) have it in a using clause that will "soft close" the connection but next time you are likely to get an already established connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文