MS DAAB 4.1 数据库对象线程安全吗?

发布于 2024-09-08 05:53:35 字数 83 浏览 2 评论 0原文

我遇到过一些具有单例的代码,它创建/重用 MSDAAB 数据库对象的静态实例。数据库对象创建后线程安全吗?我在 MSDAAB 文档中找不到任何一种方式。

I've come across some code that has a singleton which creates / reuses a static instance of the MSDAAB Database object. Is the Database object threadsafe after creation? I couldn't find anything one way or the other in the MSDAAB docs.

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

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

发布评论

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

评论(1

漫雪独思 2024-09-15 05:53:35

尽管这个答案,我认为正式的答案是否定的。

我之所以这么说,是因为Database 将存储过程参数缓存在名为parameterCache 的实例变量中。

将项目添加到缓存中,也可以使用 ClearParameterCache() 方法清除缓存,而无需首先获取 parameterCache 上的锁。

如果调用 ClearParameterCache(),则可能会遇到这样的情况:一个线程认为缓存中有一个项目,然后另一个线程清除缓存,并且当第一个线程去检索它拥有的项目时已被删除并引发异常。

好消息是,如果 ClearParameterCache() 从未被调用,那么我预计最坏的情况是,最初参数可能会多次派生并多次添加到缓存中(通过不同的线程)。我还没有测试过它,但看起来它应该仍然有效(尽管效率很低)。

因此,实际上,只要您不调用 ClearParameterCache() 我认为您应该没问题。

Despite this answer, I think the formal answer is no.

The reason why I say that is that Database caches stored procedure parameters in an instance variable called parameterCache.

Items are added to the cache and the cache can also be cleared using the ClearParameterCache() method without first obtaining a lock on the parameterCache.

If ClearParameterCache() is called, then it is possible to hit situations where one thread thinks there is an item in the cache then another thread clears the cache and when the first thread goes to retrieve the item it has been removed and an exception is thrown.

The good news is that if ClearParameterCache() is never called then the worst I would expect is that initially the parameters could be be derived multiple times and added to the cache multiple times (by different threads). I haven't tested it but it looks like it should still work (although it is inefficient).

So, practically, as long as you don't call ClearParameterCache() I think you should be OK.

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