在 .NET 中管理数据库连接的最佳实践是什么?

发布于 2024-09-09 17:13:11 字数 567 浏览 1 评论 0原文

关于在 .NET 应用程序中管理数据库连接的最佳实践——我知道,一般来说,传递连接对象是不好的。

然而,我有一些具体的好奇心:


1。我有两个业务实例 不同类别的对象,在一个 亲子关系(孩子 是私人的。)以下哪一项 最好?

  • 保持一个私有静态连接打开和共享,由两个对象使用,并保持打开状态,直到父对象被释放。

    保持
  • 保持两个私有静态连接打开,每个对象一个,不要 关闭直到该对象被处理。

  • 不要保持静态连接;打开并随后关闭一个新的 每个需要它的方法的连接。然而,我的大多数方法只运行 1-3 个查询,所以这看起来效率很低......?


2.我的第二个问题本质上是相同的,但针对的是单一形式。这里最好的是什么?

  • 在表单的生命周期内保持一个私有静态连接打开和共享。

  • 不要保持静态连接;为需要连接的表单中的每个方法打开并随后关闭连接(同样,每个方法只需 1-3 个查询。)

Regarding best practice for managing database connections in a .NET application -- I know that, in general, it's bad to pass around a connection object.

However, I have some specific curiosities:


1. I have two instances of business
objects, of different classes, in a
parent-child relationship (the child
is private.) Which of the following
is best?

  • Keep one private static connection open and shared, used by both objects, and left open until the parent is disposed.

  • Keep two private static connections open, one for each object, not to be
    closed until the object is disposed.

  • Do not keep static connections; open and subsequently close a new
    connection for every method that requires it. However, most of my methods only run 1-3 queries, so this seems inefficient... ?


2. My second question is essentially the same, but for a single form. What's best here?

  • Keep one private static connection open and shared for the lifetime of the form.

  • Do not keep a static connection; open and subsequently close a connection for every method in the form that requires it (again, a mere 1-3 queries per method.)

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

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

发布评论

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

评论(4

泡沫很甜 2024-09-16 17:13:11

(是一条评论)...

理论上,您不应该从业务逻辑访问数据库 - 它应该位于单独的数据访问类中。 (例如,将来您需要将它们离线存储在 XML 中,或者使用 Oracle 而不是 SQL Server...您不想重写您的业务逻辑!)

您的业务对象不应具有与他们。应在某些 DAL 工厂类型方法中打开连接,检索/构建对象,然后关闭连接并返回对象。

业务对象本身应该包含业务逻辑字段和方法,它们可能会回调数据访问层,数据访问层应该为每个 DAL 方法创建一个新的数据库连接。

使用连接池可以消除您对效率低下的担忧,这意味着如果您打开和关闭一个连接数百次,那么它们很可能都会使用同一个连接。但是您根本不应该保持数据库连接闲置 - 尤其是作为类的成员。

希望有帮助!

(Was a comment)...

The theory is you should not be accessing the database from your business logic - it should be in a separate data-access class. (Say for example in the future you need to store them offline in XML, or use Oracle rather than SQL Server... you don't want to re-write your business logic!)

Your business objects should not have database connections associated with them. The connection should be opened in some DAL factory-type method, the object retrieved/built, then the connection closed and the object returned.

The business objects themselves should contain business logic fields and methods, which might call back to the Data Access Layer, which should create a new database connection for each DAL method.

Your inefficiency fears can be put to rest by using Connection Pooling, which means that if you open and close a connection hundreds of times, chances are they will all use the same one. But you should not be keeping database connections hanging around at all - especially not as members on a class.

Hope that helps!

疧_╮線 2024-09-16 17:13:11

我的理解是,连接应该只在需要时保持开放。大多数时候我在Using语句中看到连接,类似于

using (DBConnection db = new DBConnection(connectString))
{
    //do stuff
}

My understanding is that connections should only stay open as long as needed. Most of the time I've seen connections in Using statements, similar to

using (DBConnection db = new DBConnection(connectString))
{
    //do stuff
}
囚你心 2024-09-16 17:13:11

使用 ADO.NET 的最佳实践

此链接可能会有所帮助 有趣的摘录。

为了获得最佳性能,请保持连接
仅当数据库打开时
必需的。另外,减少数量
打开和关闭连接的次数
用于多个操作。

我一直遵循在 using 块中打开连接的做法,以便始终调用 Dispose 方法(以及 Close 方法),而无需我担心。使用这种方法,我从未遇到过性能不佳与过多的并发连接或过多的设置或拆卸操作相关的情况。

This link may be helpful: Best Practices for Using ADO.NET

Here's an interesting excerpt.

For best performance, keep connections
to the database open only when
required. Also, reduce the number of
times you open and close a connection
for multiple operations.

I've always followed the practice of opening connections in a using block, so that the Dispose method (and hence the Close method) is always called without my worrying about it. Using this approach I've never encountered a situation where poor performance was linked either to excessive concurrent connections or excessive setup or tear down operations.

弥繁 2024-09-16 17:13:11

为了回答这两个问题,如果您使用的是具有连接池的东西(例如 ADO.NET),您应该对查询进行编码以保持连接打开尽可能短。即为每个需要它的方法打开并随后关闭一个新连接。。当您关闭连接时,它将返回到连接池并在后续查询中重用,因此您不会因打开和关闭一堆连接而导致性能损失。优点是您不会冒忘记关闭的连接泄漏的风险,从长远来看,与长时间保持连接打开相比,您同时打开的连接会更少。应用程序是 Windows 窗体还是 Web 窗体并不重要:保持连接打开尽可能短。

In answer to both questions, if you are using something that has connection pooling, like ADO.NET, you should code your queries to keep the connection open as short as possible. I.e. open and subsequently close a new connection for every method that requires it.. When you close the connection it will be returned to the connection pool and reused on a subsequent query and thus you will not incur a performance penalty by opening and closing a bunch of connections. The advantage is that you won't risk leaking connections that you forgot to close and in the long run, you'll have fewer simultaneous connections open than if you keep connections open for long periods of time. It doesn't matter whether the application is a Windows form instead of a Web form: keep connections open as short as possible.

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