0

The following two code snippet, code snippet 1 executes fine, it uses using for transaction, which can automatically execute dispose immediately after it is out of scope, while code snippet 2 removes using for transaction(my test run looks fine), is there any problem after removing it? Can I use snippet 2 instead of snippet 1?

UPDATE: I tested snippet 2 many times, including using multithreading, but I didn't seem to find any problems. In snippet 2, using is used for connections, but not transactions. Even if the transaction executes an exception, it is not a problem because using on the outer connection will occur: When the connection is automatically disposed, the transaction is also disposed. Is this actually a wrong understanding? What is the correct understanding?

code snippet 1:

using(IDbConnection connection = new SqlConnection(connStr))
{
    connection.Open();

    using(IDbTransaction transaction = connection.BeginTransaction())
    {
       // todo: Insert operation
       ...
       // todo: Update operation
       ...
       
       transaction.Commit();
    }
}

code snippet 2:

using(IDbConnection connection = new SqlConnection(connStr))
{
    connection.Open();

    IDbTransaction transaction = connection.BeginTransaction();
    
    // todo: Insert operation
       ...
    // todo: Update operation
       ...
       
    transaction.Commit(); 
}
  • So you want to deliberately forgo disposing an object that obviously has unmanaged resources for what reason exactly? If you really want to test this, run this code a few hundred times, and for extra credit, with some degree of parallelism, maybe throw and swallow some exceptions for good measure – TheGeneral Dec 08 '21 at 02:47
  • Code snippet 2 removes using for transaction, my test run looks fine. I think if the connection is dispose, the transaction will be dispose, but I'm not sure I'm getting this right. Is there any problem after removing it? –  Dec 08 '21 at 02:51
  • Sorry,It didn't solve my question,I'm trying to figure out this detail:When the connection is automatically disposed, the transaction is also disposed. Is this actually a wrong understanding? –  Dec 08 '21 at 03:03
  • @gunr2171 I've updated the question to be a little more specific. –  Dec 08 '21 at 03:19
  • @TheGeneral I've updated the question to be a little more specific. –  Dec 08 '21 at 03:20
  • Yes, your update was worthy, I honestly don't know the answer and tracking what is happening in the source is not as straight forward as my attention span would like. Given this, my answer would be. At your own risk. However with 1 line using declarations I don't see why you would risk it, or push bad habits on to new coders maintaining your code base – TheGeneral Dec 08 '21 at 03:23
  • 1
    See [Does SqlConnection.Dispose() perform a rollback on all pending transactions?](https://stackoverflow.com/questions/13850165) and [What happens to an uncommitted transaction when the connection is closed?](https://stackoverflow.com/questions/1539564). – gunr2171 Dec 08 '21 at 03:31
  • @gunr2171 Thank you. Actually, I used to use snippet 1 all the time, but my intention was to figure out if snippet 2 was going to cause a problem, to figure out once and for all what the problem was, and if it didn't cause a problem, there was no need to follow suit. –  Dec 08 '21 at 03:34

0 Answers0