数据访问对象:单例还是许多小对象?

发布于 2024-08-12 08:51:53 字数 276 浏览 7 评论 0原文

在开发需要进行大量数据访问的应用程序(Web、Win 等)时,最好在请求期间保持数据访问对象处于打开状态(即连续执行许多操作,然后在完成后将其关闭) ,或者继续打开和关闭新的?

protected aDataContext dc = new aDataContext();

vs

private aObject GetInfo(...) {...}

我认为前者的性能会更好;但这似乎是一个不好的做法。

When developing an application (web, win, whatever) which does alot of data access, is it better to keep your data access object open for the length of the request (i.e. do many things in a row, then close it when you finish), or keep opening and closing new ones?

protected aDataContext dc = new aDataContext();

vs

private aObject GetInfo(...) {...}

I would think the former would be better for performance; but it seems like a bad practice.

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

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

发布评论

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

评论(2

清醇 2024-08-19 08:51:53

最好使用连接池。
尽可能保持有限数量的连接打开。
打开一个连接的成本很高,但拥有数百万个打开的连接可能会杀死你的服务器。
您应该对您的场景进行基准测试以获得最佳结果......

Better use a connection pool.
keep a limited number of connections open as much as you can.
Opening a connection is pricey, yet having million open connection might kill your server.
you should benchmark your scenario for best results....

狼性发作 2024-08-19 08:51:53

通常,您应该为每个工作单元打开一个新连接,使用它,然后尽快关闭它。只要连接字符串相同,.NET(或 ADO 或 ODBC 或其他)内部就会为您池化连接,因此它实际上非常高效。还有其他问题需要考虑 - 特别是事务 - 但一般来说,最好遵循这种打开-执行-关闭的模式,并让 .NET 管理连接池。

Typically you should open a new connection for every unit of work, use it and then close it as soon as possible. Internally .NET (or ADO or ODBC or whatever) will pool connections for you, so long as the connection string is the same, therefore it's actually very efficient. There are other issues to take into account - particularly transactions - but in general it's best to follow this pattern of open-do-close and let .NET manage the connection pool.

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