Why use a using statement with a SqlTransaction?

前端 未结 9 1359
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 11:27

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.<

相关标签:
9条回答
  • 2020-12-02 11:30

    In the end, using is just a shortcut for a pattern. But it's a very useful and helpful shortcut, because it makes sure 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?

    0 讨论(0)
  • 2020-12-02 11:37

    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();
    }
    
    0 讨论(0)
  • 2020-12-02 11:38

    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.

    0 讨论(0)
  • 2020-12-02 11:40

    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.

    0 讨论(0)
  • 2020-12-02 11:40

    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.

    0 讨论(0)
  • 2020-12-02 11:40

    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

    0 讨论(0)
提交回复
热议问题