52

I've been running into some problems concerning a SqlTransaction I'm using in my code. During my Googling I see many people using a using statement with a SqlTransaction.

What is the benefit and/or difference of using this type of statement with a SqlTransaction?

using (SqlConnection cn = new SqlConnection())
{
     using (SqlTransaction tr = cn.BeginTransaction())
     {
      //some code
      tr.Commit();
     }
}

Currently my code looks like this:

SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["T3"]);
cn.Open();
SqlTransaction tr = cn.BeginTransaction();

try
{
     //some code
     tr.Commit();
     cn.Close();
}
catch(Exception ex)
{
      tr.Rollback();
      cn.Close();
      throw ex;
}

What is the advantage of one way over the other?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
MDStephens
  • 1,013
  • 2
  • 9
  • 10
  • 7
    For "less" nesting, I believe you can skip the first pair of curly brackets. (or is it braces...) – Svish Jul 14 '09 at 20:38

8 Answers8

62

A using statement should be used every time you create an instance of a class that implements IDisposable within the scope of a block. It ensures that the Dispose() method will be called on that instance, whether or not an exception is thrown.

In particular, your code only catches managed exceptions, then destroys the stack frame by throwing a new exception instead of rethrowing the existing one.

The correct way to do it is:

using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["T3"])) {
    cn.Open();
    using (SqlTransaction tr = cn.BeginTransaction()) {
        //some code
        tr.Commit();
    }
}

Note that if your class has instance members of types that implement IDisposable, then your class must implement IDisposable itself, and dispose of those members during its own Dispose() call.

Michael
  • 8,362
  • 6
  • 61
  • 88
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 4
    ....because it guarantees that the "Dispose" method of the IDisposble interface being implemented will be called - no matter what happens in your code. – marc_s Jul 14 '09 at 20:31
  • 3
    +1, generally correct, but you can't _always_ use a using statement, sometimes you need to implement IDisposable yourself. I would say: "Whenever possible", which means "whenever creating an IDisposable instance used only within a bloc" or something like this. – Stefan Steinegger Jul 14 '09 at 20:33
  • 1
    That makes sense. I've also corrected my exception. Thanks for pointing that out. – MDStephens Jul 14 '09 at 20:44
  • 3
    @Downvoter: Care to say why? otherwise you're pretty irrelevant. – John Saunders Aug 03 '09 at 20:23
  • 3
    Yes, if `Dispose` is called on the `SqlTransaction` before a `Commit`, then the transaction will be rolled back. Of course, `Dispose` will be called if an exception is thrown within the block and not handled. – John Saunders Aug 19 '11 at 19:32
  • 14
    Do we have to explicitly declare tran.rollBack() inside a catch or will this be handled by the using block? – Zo Has Jun 11 '12 at 07:45
  • 5
    @ZoHas it will be handled in the Dispose – Erik Bergstedt Nov 25 '15 at 11:50
  • I think we still need to add Rollback in catch so as to make sure we Rollback immediately and not wait for GC. – Mukus Jul 22 '21 at 23:07
  • 1
    @Mukus you won't need to call `Rollback` explicitly. If the transaction hasn't been committed by the time that `Dispose` is called, then `Rollback` will be called for you. – John Saunders Jul 26 '21 at 06:55
  • @JohnSaunders But the point is it will not get called immediately. – Mukus Jul 26 '21 at 07:01
  • @Mukus yes, it will be called immediately, at the end of the `using` block. The end of the `using` block calls the `Dispose` method. If the `Dispose` method sees that the transaction has not been committed or rolled back, then it will immediately call the `Rollback` method. – John Saunders Jul 26 '21 at 07:04
30

The reason for this is that the SqlTransaction object will roll back in its Dispose() method if it was not explicitly committed (e.g. if an exception is thrown). In other words, it has the same effect as your code, just a little bit cleaner.

Ken Keenan
  • 9,818
  • 5
  • 32
  • 49
  • 3
    Confirmed this by de-compiling. Calls this.Rollback() on this.Dispose(). – ddotsenko Sep 01 '11 at 19:11
  • 7
    Actually, whether or not Rollback is called inside Dispose() dependent on the implementation of the driver you're using (see http://msdn.microsoft.com/en-us/library/bf2cw321(v=vs.110).aspx). Driver implementors are supposed to call Rollback, but Microsoft recommends not counting on it. So if you know the only driver you'll ever use does call Rollback inside Dispose() you're safe. Otherwise, it's safer to explicitly call it. – d512 Aug 20 '14 at 20:00
16

Essentially the using does the same thing that you are doing, except int a finally block instead of catching all exceptions:

using (SqlConnection cn = new SqlConnection())
{
     using (SqlTransaction tr = cn.BeginTransaction())
     {
      //some code
      tr.Commit();
     }
}

is the same as, just much less code :)

{
    SqlConnection cn = null;
    try
    {
       cn = new SqlConnection();
       {
           SqlTransaction tr = null;
           try
           {
               tr = cn.BeginTransaction())

               //some code
               tr.Commit();
            }
            finally
            {
                if(tr != null && tr is IDisposable)
                {
                    tr.Dispose();
                }
            }
        }
    }
    finally
    {
        if(cn != null && cn is IDisposable)
        {
            cn.Dispose();
        }
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
heavyd
  • 17,303
  • 5
  • 56
  • 74
  • You actually have an extra set of braces in your first example: you can nest using statements without creating a new block, e.g. `using (x = new X) using (y = new Y) { }` – Robert Rossney Jul 15 '09 at 01:03
  • 3
    Just being explicit, same reason we always put braces around all our if statements, even if they're one-liners – heavyd Jul 15 '09 at 02:07
  • 2
    You should also do `cn.Open();` before `using (SqlTransaction tr ...`. Otherwise you will receive InvalidOperationException. Or am I missing something? – Laurynas Lazauskas Jul 13 '16 at 09:55
9

In the end, using is just a shortcut for a pattern. But it's a very useful and helpful shortcut, because it ensures you implement the pattern correctly and means you can do it with less code.

In this case, you haven't implemented the pattern correctly. What happens in your code if the call to tr.RollBack() also throws an exception?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
4

The using statement is closing and disposing your connection and transaction for you. It's the equivalent of having a finally block on your try/catch that does the dispose.

You could also condense the using blocks down a bit like this...

using (SqlConnection cn = new SqlConnection())
using (SqlTransaction tr = cn.BeginTransaction())     
{
      //some code
      tr.Commit();
}

which would be roughly the same as:

SqlConnection cn = null;
SqlTransaction tr = null;
try
{
    cn = new SqlConnection());
    tr = cn.BeginTransaction());

    //some code
    tr.Commit();
}
finally
{
    if (cn != null)
        cn.Dispose();
    if (tr != null)    
        tr.Dispose();
}
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
  • Very close, but there's also an additional anonymous scope block in there. The code as you have it won't compile, because cn and tr are out of scope in the finally block. – Joel Coehoorn Jul 14 '09 at 20:36
  • 2
    @ZoHas it will automatically Rollback at `Dispose();` if there was none call of `Commit();` [see also](http://stackoverflow.com/a/641669/1993545) – WiiMaxx Jan 09 '15 at 10:27
3

If you don't use a using() block, you'll have to explicitly call the .Dispose() method of the SqlConnection and SqlTransaction objects. If you fail to do that, then unmanaged resources will not be released and could cause memory leaks or other problems.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
1

In addition to all that, it prettifies your code. Doesn't the 7 lines of code look better than the 14 lines? I breath a sign of relief every time I see a using block. It's like that little squirt of mist that comes out of that glad smelly thing. Mmm, I'm a pretty block of efficient code. Look at how well I manage memory and how pleasing I am to the eye.

Levitikon
  • 7,749
  • 9
  • 56
  • 74
1

Using using gurantees that your connection object will be disposed after the code returns. Dispose is useful to release unmanages resources, As a good practice, if an object implements IDisposable, dispose method always should be called

jyotishka bora
  • 3,963
  • 2
  • 23
  • 19