MS DAAB 4.1 数据库对象线程安全吗?
我遇到过一些具有单例的代码,它创建/重用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管这个答案,我认为正式的答案是否定的。
我之所以这么说,是因为
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 calledparameterCache
.Items are added to the cache and the cache can also be cleared using the
ClearParameterCache()
method without first obtaining a lock on theparameterCache
.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.