.NET中数据库连接中的Singleton模式

发布于 2025-01-26 10:32:31 字数 111 浏览 1 评论 0 原文

在我的项目中有疑问,数据库连接对象是通过Singleton模式创建的,因为打开和关闭连接是沉重的操作,但我怀疑我们不应该处理该对象,因为关闭连接Frees Frees Memory的使用是什么用来使其保持生存

Have a doubt in my project the database connection object is created via singleton pattern as opening and closing connection is heavy operation but i have doubt shouldn't we dispose of the object what is the use of keeping it alive as closing connection frees memory

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

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

发布评论

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

评论(2

勿挽旧人 2025-02-02 10:32:31

确切地说, 保持与数据库服务器的连接。所有DB客户端[ADO.NET,ODBC,JDBC等]支持连接池机制。您最好依靠它并适当地使用它,而不是将单身连接对象保留到数据库中。

Exactly, there is no point in holding onto a connection to your db server. All DB clients[ADO.NET, ODBC, Jdbc,etc] support connection pool mechanism. You are better off to rely on that and use that properly, than to keep a singleton connection object to your database.

桃酥萝莉 2025-02-02 10:32:31

好像仿佛没有正确实现单身人士,那么一个线程可以处理连接,但是另一个线程可以尝试执行某些命令。

当您调用新的SQLConnection() condect> connection.open()时

,最好创建新连接以利用连接池。

It is not good idea as if singleton is not correctly implemented, then one thread can dispose the connection, however the another thread can try to execute some command.

There is connection pooling when you call new SqlConnection() or connection.Open()

So it is better to create new connection to take advantage of connection pooling.

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