.NET ODBC 连接池

发布于 2024-07-06 12:43:09 字数 350 浏览 4 评论 0原文

我像这样打开一个连接:

Using conn as New OdbcConnection(connectionString)
    conn.Open()
    //do stuff
End Using

如果启用了连接池,则该连接不会物理关闭,而是释放到池中并将被重用。 如果它被禁用,它将被物理关闭。

有什么方法可以以编程方式知道是否连接池已启用? 以及池中当前打开的已使用和未使用连接的数量?

编辑:我需要从程序内部获取此信息,我无法在将部署该程序的每台 PC 上手动检查它。

I open a connection like this:

Using conn as New OdbcConnection(connectionString)
    conn.Open()
    //do stuff
End Using

If connection pooling is enabled, the connection is not physically closed but released to the pool and will get reused. If it is disabled, it will be physically closed.

Is there any way of knowing programmatically if connection pooling is enabled or not? and the number of used and unused connections currently open in the pool?

EDIT: I need to get this information from within the program, I can't go and check it manually on every single PC where the program will be deployed.

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

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

发布评论

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

评论(3

不即不离 2024-07-13 12:43:09

MSDN 对此的深入指南

配置连接池
数据源管理员

[截图]

或者,您可以启动 ODBC
运行时的数据源管理员
迅速的。 在任务栏上,单击“开始”,
单击“运行”,然后输入 Odbcad32。

用于管理连接的选项卡
池化位于 ODBC 数据中
源管理员对话框
ODBC 3.5 及更高版本。
配置连接池
注册表

对于 3.5 版之前的版本
ODBC核心组件,你需要
直接修改注册表即可
控制连接池
CPTimeout 值。

池化始终由数据服务器软件处理。 重点是,在 .NET 中您不必担心它(例如,这就是为什么在使用 SQL Server 时应始终使用 SqlConnection - 部分原因是它启用了连接池)。

更新

在 Vista 上,只需在“开始”菜单中键入“ODBC”,它就会为您找到该应用程序。

OP 澄清后的更新

在确定是否在每台计算机上启用连接池方面,请查看 MSDN 指南 我想说你最好检查一下注册表值(参见

不过,除非客户端机器真的很糟糕,否则我可能根本不会打扰。据我所知,它是默认启用的,并且在客户端计算机上打开连接(根据我的经验)从来都不是一件大事。 只有当很多被打开时,它才真正成为一个大问题。

MSDN as in-depth guidelines on this

Configuring Connection Pooling from
the Data Source Administrator

[snip]

Alternatively, you can start the ODBC
Data Source Administrator at the Run
prompt. On the taskbar, click Start,
click Run, and then type Odbcad32.

The tab for managing connection
pooling is found in the ODBC Data
Source Administrator dialog box in
version ODBC 3.5 and later.
Configuring Connection Pooling from
the Registry

For versions prior to version 3.5 of
the ODBC core components, you need to
modify the registry directly to
control the connection pooling
CPTimeout value.

Pooling is always handled by data server software. The whole point being is that in .NET you shouldn't have to worry about it (for example, this is why you should always use the SqlConnection when working with SQL Server - part of it is that it enables the connection pooling).

Update

On Vista, just type "ODBC" in the Start menu and it will find the app for you.

Update Following Clarification from OP

In terms of determining if connection pooling is enabled on each machine, looking at the MSDN guidelines I whould say you would best best if you check the registry values (See this article for pointers on registry access).

TBH though, unless the client machines are really crappy, I would possibly not even bother.. AFAIK it is enabled by default, and opening connections on a client machine (in my experience) has never been a big deal. It only really becomes a big deal when lots are being opened.

凉月流沐 2024-07-13 12:43:09

看起来您可以读取此注册表项:

[HKEYLOCALMACHINE]\SOFTWARE\ODBC\ODBCINST.INI\SQL Server\CPTimeout

(或其某些变体,具体取决于您的操作系统和用户帐户)。 如果该值为 0,则禁用连接池。 如果它是任何大于 0 的值,则表示已启用。

请参阅:

http://msdn.microsoft.com/en-us/library/ ms810829.aspx

我不确定是否获取打开的连接数。 只是好奇:为什么你需要知道这个数字?

Looks like you can just read this registry key:

[HKEYLOCALMACHINE]\SOFTWARE\ODBC\ODBCINST.INI\SQL Server\CPTimeout

(or some variant thereof, depending on your OS and user account). If the value is 0, then connection pooling is disabled. If it's any value above 0, it's enabled.

See:

http://msdn.microsoft.com/en-us/library/ms810829.aspx

I'm not sure about getting the number of open connections. Just curious: why do you need to know the number?

沙沙粒小 2024-07-13 12:43:09

要确定每个数据库上打开的连接数,请尝试此 sql - 我从互联网上的文档中获取它

select  db_name(dbid) , count(*) 'connections count'
  from master..sysprocesses
 where spid > 50 and spid  @@spid
 group by  db_name(dbid)
 order by count(*) desc

Spid <= 50 由 sqlserver 使用。 所以上面的sql会告诉你程序使用的连接。

To determine the number of open connections on each db, try this sql - I got it from a document on internet

select  db_name(dbid) , count(*) 'connections count'
  from master..sysprocesses
 where spid > 50 and spid  @@spid
 group by  db_name(dbid)
 order by count(*) desc

Spids <=50 are used by sqlserver. So the above sql would tell you the connection used by your programs.

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