SqlConnection 单例

发布于 2024-08-23 19:54:55 字数 817 浏览 4 评论 0原文

您好,我想问创建 Singleton 以便只有一个与数据库的活动连接是否是一个好主意。我想做的是: 1)我有一个wcf服务 2)wcf服务从db获取数据 3)我想创建一个像这样的单例,只有一个到数据库的连接:

private static PersistanceSingleton _Instance;
    public static PersistanceSingleton Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = new PersistanceSingleton();
            }
            return _Instance;
        }
    }

我知道这不是一个理想的单例,但我只是为了这篇文章的目的而编写它。 我想在这里有一些持久性存储库,我将在构造函数中实例化它们。 在我的服务类中,我将在构造函数中包含以下代码

_DBPersistanceSingleton = PersistanceSingleton.Instance;

然后,当出现某些请求(例如 GetUsersRequest)时,我想做类似的事情:

_DBPersistanceSingleton.GetUsers()

在执行每次对 db 的调用之前,我还将检查 SqlConnection 是否打开。 请告诉我这是否是一个好的做法。我考虑这个解决方案的原因是因为大量用户将通过客户端应用程序连接到该服务

Greetings, I would like to ask if creating Singleton to have only one active connection to db is a good idea. What i would like to do is:
1) I have a wcf service
2) wcf service gets data from db
3) i would like to create a singleton like this to have only one connection to db:

private static PersistanceSingleton _Instance;
    public static PersistanceSingleton Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = new PersistanceSingleton();
            }
            return _Instance;
        }
    }

I know this is not an ideal singleton but i just wrote it for this post purpose.
I would like to have some persistance repositories here and which I will be instantiating them in constructor.
Inside my service class I would have the following code inside constructor

_DBPersistanceSingleton = PersistanceSingleton.Instance;

Then when some request comes (e.g. GetUsersRequest) i would like to do something like:

_DBPersistanceSingleton.GetUsers()

Before each call to db is executed I will also check whether SqlConnection is open or not.
Please let me know if this is a good practice. The reason why I think about this solution is because of large number of users that will be connecting to that service via client application

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

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

发布评论

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

评论(3

流年里的时光 2024-08-30 19:54:56

像这样重用SqlConnection并不是一个好习惯。需要时打开它,用完后立即关闭它。连接池将在幕后为您工作,重用连接。

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.

伪装你 2024-08-30 19:54:56

不,我强烈建议你不要这样做。如果同时收到多个请求会发生什么情况?他们不能同时使用相同的连接,最多只会引入一个大瓶颈。

连接池会自动为您处理,消除您的麻烦,因此您无需担心。只需根据需要打开和关闭连接即可。

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.

无戏配角 2024-08-30 19:54:56

将 sql 连接放在一边...

这种单例模式不是线程安全的,并且在多线程应用程序中使用是一个坏主意(因为您的 WCF 服务可能是这样)。

使用此代码,如果多个同时请求到达,则可能会创建多个实例。

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.

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