在 C# 中创建新 SqlConnection 的开销

发布于 2024-08-16 23:01:04 字数 887 浏览 1 评论 0原文

不久前,我为我的 .net 应用程序编写了一个 ORM 层,其中所有数据库行都由 DatabaseRecord 的子类表示。有许多方法,例如 Load()Save() 等。在我的初始实现中,我在 DatabaseRecord< 的构造函数中创建了与数据库的连接。 /code> 例如,

connection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString
);

然后我在访问数据库的方法的开头和结尾处对该 SqlConnection 调用 Open()Close() 。在我(作为一个熟悉编程但对 c# 和 .net 不熟悉的人)看来,这是最有效的做事方式 - 拥有一个连接,并在类中必要时打开/关闭它。

我刚刚做了一些阅读,似乎在很多地方都推荐了这种模式:

using (var connection = new SqlConnection(...)) {
    connection.Open();
    // Stuff with the connection
    connection.Close();
}

我可以理解为什么它是可取的 - 连接会自动Dispose()d,即使你的东西中间的 do 会导致未捕获的异常。我只是想知道像这样多次调用 new SqlConnection() 的开销是多少。

连接池已启用,因此我认为开销是最小的,第二种方法应该是最佳实践,但我只是想确保我的假设是正确的。

A while back I wrote an ORM layer for my .net app where all database rows are represented by a subclass of DatabaseRecord. There are a number of methods like Load(), Save() etc. In my initial implementation I created a connection to the DB in the constructor of DatabaseRecord e.g.

connection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString
);

I then call Open() and Close() on that SqlConnection at the beginning and end of my methods which access the database. This seemed to me (as someone who was familiar with programming but new to c# and .net) to be the most efficient way to do things - have one connection and open/ close it where necessary within the class.

I've just been doing some reading though and it appears that this pattern is recommended in a number of places:

using (var connection = new SqlConnection(...)) {
    connection.Open();
    // Stuff with the connection
    connection.Close();
}

I can see why it's desirable - the connection is automatically Dispose()d even if the stuff you do in the middle causes an uncaught exception. I was just wondering what the overhead is for calling new SqlConnection() potentially many times like this.

Connection Pooling is on so I imagine the overhead is minimal and the second approach should be best practice but I just wanted to make sure my assumptions are right.

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

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

发布评论

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

评论(3

滥情空心 2024-08-23 23:01:04

是的,这是最佳实践。 using 使您对 Close() 的调用是异常安全的。

创建(任何)对象的开销确实是最小的,对于短命对象(停留在 GC 第 0 代)来说也是最小的。

请注意,您不必再在 using 块末尾调用 Close(),它会自动为您完成(Dispose==Close)。

Yes, it is best practice. The using makes your call to Close() exception-safe.

And the overhead of creating a (any) object is indeed minimal, and smallest for short-lived objects (that stay in GC generation 0).

Note that you don't have to call Close() at the end of the using-block anymore, it is automatically done for you (Dispose==Close).

锦欢 2024-08-23 23:01:04

这部分是一个品味问题。只要使用连接池,创建新连接(回收池连接)的开销就会很小,因此通常推荐的模式是根据需要创建新连接对象。

如果您立即运行多个命令,那么我认为没有理由为每个命令创建新连接,但您应该避免长时间保持打开的连接。

另外,您应该注意 Dispose 方法将为您关闭连接。因此无需同时调用 CloseDispose。由于 using 子句在结束时将调用 dispose,因此通常不需要调用 Close

This is partially a matter of taste. As long as you employ connection pooling the overhead of creating a new (recycling a pooled connection) will be minimal, so generally the recommended pattern is to create new connection objects as needed.

If you run several commands immediately after each other then I see no reason to create new connections for each of them, but you should avoid holding on to open connections for a long time.

Also, you should note that the Dispose method will close the connection for you. So there is no need to call both Close and Dispose. Since the using clause will call dispose when it ends there is normally no need to call Close.

如何视而不见 2024-08-23 23:01:04

如果您不确定打开/关闭连接的成本,请将 SqlConnection 作为类的成员变量,但使类 IDisposable 并在以下情况下释放 SqlConnection:类被处置

If you're unsure about the cost of opening/closing connection, have the SqlConnection a member variable of your class, but make the class IDisposable and dispose of the SqlConnection when the class is disposed

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