ADO.NET,在没有事先提交或回滚的情况下关闭 OracleConnection:会泄漏吗?

发布于 2024-07-18 03:33:05 字数 999 浏览 14 评论 0原文

假设我正在执行以下操作:

using (OracleConnection conn = new OracleConnection(connStr))
{
    OracleTransaction trans = conn.BeginTransaction();
    OracleCommand command = new OracleCommand(cmdTxt, conn, trans);
    // this statement is executed in a transaction context:
    command.ExecuteNonQuery();
}
// the using statement will dispose and thus close the connection.
// a rollback is done implicitly

虽然我没有执行 transaction.Rollback(),但我的测试表明回滚是隐式完成的。

我的问题是:这段代码会泄漏连接或其他什么吗?

Edit1:我是 System.Data.OracleClient 命名空间。

Edit2:这是一个人为的示例代码。 更现实的场景是在 using 语句中发生异常并且 Commit() 语句尚未执行。

Edit3:从答案来看,我认为这是有利的:

using (OracleConnection conn = new OracleConnection(connStr))
using (OracleTransaction trans = conn.BeginTransaction())
using (OracleCommand command = new OracleCommand(cmdTxt, conn, trans))
{
    command.ExecuteNonQuery();
    trans.Commit();
}

应该干净地处理任何东西并弄清楚正在发生的事情。

Suppose I am doing the following:

using (OracleConnection conn = new OracleConnection(connStr))
{
    OracleTransaction trans = conn.BeginTransaction();
    OracleCommand command = new OracleCommand(cmdTxt, conn, trans);
    // this statement is executed in a transaction context:
    command.ExecuteNonQuery();
}
// the using statement will dispose and thus close the connection.
// a rollback is done implicitly

Although I did not executed transaction.Rollback(), my tests showed that a rollback is done implicitly.

My question is: Will this code leak connections or anything else?

Edit1: I am the System.Data.OracleClient namespace.

Edit2: This is a contrieved example code. The more realistic scenario is when within the using statement an exception occures and the Commit() statement is not executed yet.

Edit3: From the answer I think that this is favorable:

using (OracleConnection conn = new OracleConnection(connStr))
using (OracleTransaction trans = conn.BeginTransaction())
using (OracleCommand command = new OracleCommand(cmdTxt, conn, trans))
{
    command.ExecuteNonQuery();
    trans.Commit();
}

Should cleanly dispose anything and make clear what is happening.

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

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

发布评论

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

评论(1

一笔一画续写前缘 2024-07-25 03:33:05

它不会泄漏。 using 子句保证 OracleConnection 将被释放,无论命令是成功完成还是因异常而失败,并且它将带走事务。

但由于 OracleTransaction 是 IDisposable,因此在事务周围放置一个 using 子句可能是一个很好的形式,例如,

using (OracleTransaction trans = conn.BeginTransaction())
{
  // ...
  trans.Commit();
}

这将使代码的读者更清楚事务正在被清理; 特别是,如果后续增强在同一连接上执行多个事务,这一点可能会变得很重要。

另外,根据下面 John 的评论,您应该在 OracleCommand 周围放置一个 using 语句,以便可以及时清理它。

It will not leak. The using clause guarantees that the OracleConnection will be disposed, regardless of whether the command completes successfully or fails with an exception, and it will take the transaction with it.

But since OracleTransaction is IDisposable, it would probably be good form to put a using clause around the transaction as well, e.g.

using (OracleTransaction trans = conn.BeginTransaction())
{
  // ...
  trans.Commit();
}

This will make it clearer to readers of the code that the transaction is being cleaned up; in particular, it may become important if a subsequent enhancement performs multiple transactions on the same connection.

Also, as per John's comment below, you should put a using statement around the OracleCommand, so that it can be cleaned up promptly.

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