SqlConnection Singleton

大憨熊 提交于 2019-12-01 06:52:58

It's not a good practice to reuse SqlConnection like that. Open it when you need it and close it as soon as you're done with it. Connection pooling will work for you under the hood reusing the connection.

No, I'd strongly recommend you don't. What happens if multiple requests come in at the same time? They can't all use the same connection at the same, at best you'd just be introducing a big bottleneck.

Connection pooling is handled automatically for you, and takes the hassle away from you so you don't need to worry about it. Just open and close connections as needed.

Putting the sql connection aside...

This singleton pattern is not thread safe and is a bad idea to use in a multi-threaded application (as your WCF service is likely to be).

With this code, if multiple simultaneous requests arrive, it is possible that multiple instances will be created.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!