Can anyone clarify if Enterprise Library's Database object can be made static without any thread-safety issues?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes, an Database
instance is thread-safe. It is just a big factory spitting out new objects like connections. The instance itself doesn't change state after it is created. When you look at the source code you can see this clearly. The objects it produces are of course not thread-safe.
回答2:
You can, but there's no significant reason to. Database instances are cheap to create and don't hold any state.
回答3:
No don't do it. I tried it and was sorry that I did. While it was thread safe it slowed down my query requests from a web app dramatically. I thought I was being smart skirting the creation of metadata but it gummed things up instead. Now I provide a brand new instance every request and it's as happy and as fast as can be.