ADO.NET,在没有事先提交或回滚的情况下关闭 OracleConnection:会泄漏吗?
假设我正在执行以下操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不会泄漏。 using 子句保证 OracleConnection 将被释放,无论命令是成功完成还是因异常而失败,并且它将带走事务。
但由于 OracleTransaction 是 IDisposable,因此在事务周围放置一个 using 子句可能是一个很好的形式,例如,
这将使代码的读者更清楚事务正在被清理; 特别是,如果后续增强在同一连接上执行多个事务,这一点可能会变得很重要。
另外,根据下面 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.
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.