Search code examples
c#ado.nettransactionssystem.data

Why to use using statement on DbTransaction in ADO.NET / C#?


I understood the implicit rollback (that usually happens when exception happens and Dispose is called) is not guaranteed for all providers. However many examples uses:

using (DbTransactio txn = cnctn.BeginTransaction())

is there a reason for that?


Solution

  • The most plain answer there would be "because it implements IDisposable". Any type that implements IDisposable: it is your job to dispose it appropriately. In this case, with procedural code, the simplest way to do that would be via a using statement.

    In this specific case, the answer would be: because in the event of error, you want the transaction rolled back rather than left to the GC. Personally I would probably tend to use catch to Rollback, but one should hope that the Dispose() already does that. I wouldn't rely on it myself, though, unless it was documented. For example:

    using(var tran = conn.BeginTransaction()) {
        try {
            // do stuff...
            tran.Commit();
        } catch {
            tran.Rollback();
            throw;
        }
    }
    

    Note that in the related case of TransactionScope, the "Dispose() without marking it completed" is the expected way of signalling a rollback:

    using(var tran = new TransactionScope()) {
        // do stuff...
        tran.Complete();
    }