传递 SqlConnection

发布于 2024-09-02 17:16:00 字数 194 浏览 8 评论 0原文

我创建了一个 TransactionScope,并在该范围内在数据库中创建和更新了各种项目。在此过程中,我对数据库进行了大量调用。最初,我在 TransactionScope 的开头打开了一个 SqlConnection 并将其传递给任何进行数据库调用的函数,然后在所有调用完成后和事务提交之前关闭了连接。这样做更好还是为每次调用打开和关闭连接(使用相同的连接字符串)更好?

I have created a TransactionScope and within the scope various items are created and updated in the database. During this process I make a lot of calls to the database. Originally I opened a SqlConnection in the beginning of the TransactionScope and passed it around to any function that made a DB call then I closed the connection after all the calls are made and before the transaction commits. Is it better to do this or to open and close a connection (using the same connection string) for each call?

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

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

发布评论

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

评论(4

鲜肉鲜肉永远不皱 2024-09-09 17:16:00

如果您要连续进行大量调用,并且很容易传入打开的连接,那么可以重用打开的连接。

但这并不是非常重要。不像以前那么多了。 ADO.NET 可以很好地为您管理此任务。请参阅连接池。如果您的代码会变得复杂且奇怪以促进单个开放连接对象,那么这是不值得的。

(现在,确保您的连接对象(无论是否重用)被处置当然非常重要,我敢打赌您已经知道了。)

If you're going to make a lot of calls in a row, and it's easy to pass in an open connection, then yes, reuse the open connection.

This is not incredibly important though. Not as much as it used to be. ADO.NET does a good job of managing this for you. See Connection Pooling. If your code is going to get complicated and weird to facilitate one single, open connection object, it's not worth it.

(Now, making sure your connection objects (whether reused or not) are disposed of is of course really important, as I bet you already know.)

对不⑦ 2024-09-09 17:16:00

我倾向于不惜一切代价避免传递连接。例如,当其他人在一系列操作中间关闭连接时,我刚刚看到系统中会出现太多错误,因为他们不知道其他人将使用相同的连接。

由于 ADO.NET 的连接池机制,创建 SQLConnection 几乎是免费的,因此不要认为通过创建 SQLConnection 并将其传递来节省任何空间/时间。

如果像您的情况一样,您确实需要在事务范围内执行许多数据库操作,那么适当地管理您的范围,但在需要时创建、使用和处置您的连接 - 这将有助于保持你的代码更加干净和安全。

I tend to avoid passing connections around at all cost. I've just seen too many errors creep into a system when someone else, for example, closes a connection in the middle of a sequence of operations because they didn't know others were going to use the same connection.

Creating a SQLConnection is almost free due to ADO.NET's connection pooling mechanisms so don't think you're going to save any space/time by creating one and passing it around.

If, as in your situation, you truly do need to perform a number of DB operations within a transaction scope, then manage your scope appropriately, but create, use and dispose of your connections as and when you need them - it'll help keep your code A LOT cleaner and safer.

他不在意 2024-09-09 17:16:00

总的来说,我同意其他人所说的。但请注意,一旦您在单个 TransactionScope 实例中使用多个连接,它将提升您的(本地)事务 到分布式的,从而产生一些可能很大的开销。

从这个意义上说,来自同一池的多个连接(因此使用同一数据库)也算作多个连接。

如果这对您来说是一个问题,相对于代码结构或“清洁度”、典型 SQL 语句执行时间等,您需要做出决定。

In general I would agree with what others have said. Note however, that once you use multiple connections inside a single TransactionScope instance, it will promote your (local) transaction to a distributed one, thus incurring some possibly significant overhead.

Multiple connections from the same pool, thus using the same database, also count as multiple connections in this sense.

If that is an issue for you, versus code structure or "cleanness", typical SQL statement execution time, etc., you need to decide.

一页 2024-09-09 17:16:00

无论如何,如果想要与数据库建立多个连接,请使用 连接池< /a>.

Anyway if want to have several connections to DB use connection pool.

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