强制 OleDbConnection 释放文件句柄

发布于 2024-09-01 21:58:25 字数 262 浏览 5 评论 0原文

相关问题< /p>

我的代码没有即使在我对初始化的 OleDbException 调用 dispose 之后,也不会释放文件句柄。有没有办法显式强制程序释放文件句柄?

Related Question

My code doesn't release a file handle even after I call dispose to an initialized OleDbException. Is there a way to explicitly force the program to release a file handle?

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

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

发布评论

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

评论(2

夏末的微笑 2024-09-08 21:58:25

默认情况下,.NET 数据库连接使用。调用 Close()Dispose() 只是将连接释放回池中,实际上并不会强制它关闭。最终它会从池中超时,并实际上被关闭。

经过一番研究后,似乎有两种主要方法可以使其以可预测的方式关闭:

  1. 在连接字符串中禁用池 - 尝试添加 OLE DB Services = -2;,这应该为您提供除池之外的所有服务
  2. 尝试使用 OleDBConnection.ReleaseObjectPool()

对于后一种方法,您可能需要使用超时 - 摘自链接的 MSDN 文章:

请注意,单独调用该方法实际上并不会释放池中存在的活动连接。

在池最终处置之前必须发生以下操作:

  1. 调用 Close 将连接对象返回到池中。
  2. 允许每个连接对象在池外超时。
  3. 调用 ReleaseObjectPool。
  4. 调用垃圾回收。

我在工作中有一个这样的用例,其中一些内部软件需要与旧的、不灵活的、脆弱的和绝对关键的专有软件进行交互。它需要尽可能短地打开共享 MDB 数据库文件,以最大限度地减少其他软件可能“出现问题”(非常糟糕的事情)的窗口。

我计划使用连接字符串方法,因为它看起来更容易保证关闭,并且我的软件并没有真正从池中受益。

By default, .NET database connections use pooling. Calling Close() and Dispose() just releases the connection back into the pool, it doesn't actually force it to close. Eventually it will time out of the pool, and actually be closed.

After a bit of research, there seem to be two main ways to get it close predictably:

  1. Disable pooling in the connection string - try adding OLE DB Services = -2;, which should give you all services except pooling
  2. Try to make use of OleDBConnection.ReleaseObjectPool()

For the latter approach you might need to play with timeouts - excerpt from the linked MSDN article:

Note that calling the method alone does not actually release the active connections that exist in the pool.

The following must occur before the pool is finally disposed:

  1. Call Close to return the connection object to the pool.
  2. Allow each connection object to time out of the pool.
  3. Call ReleaseObjectPool.
  4. Invoke garbage collection.

I have a use-case for this at work, where some in-house software needs to interact with piece of old, inflexible, flaky and absolutely critical proprietary software. It needs to open a shared MDB database file for as short a time as possible to minimise the window where the other software might "take issue" (a Very Bad Thing).

I'm planning on using the connection string approach, as it looks to be simpler to guarantee the close, and my software doesn't really benefit from the pool.

千年*琉璃梦 2024-09-08 21:58:25

不确定为什么您的代码在调用 Dispose() 后没有关闭句柄,因为它在幕后调用 Close(),但以下内容可能会帮助您以常见的方式编写代码:

using (OleDbConnection conn = new OleDbConnection(connString))
{
    //your stuff here
    conn.Close();  //not necessary, but doesn't hurt
}

这将关闭您的句柄,无论是否是否抛出异常。使用块将在块末尾关闭/处置资源。

Not sure why your code doesn't close a handle after calling Dispose(), since that calls Close() behind the scenes, but the following may help you write the code in a manner seen commonly:

using (OleDbConnection conn = new OleDbConnection(connString))
{
    //your stuff here
    conn.Close();  //not necessary, but doesn't hurt
}

This will close your handle regardless of whether an exception was thrown or not. Using blocks will Close/Dispose resources at the end of the block.

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