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.