ado.net事务回滚而没有任何.net错误

时间:2018-11-21 13:04:18

标签: c# sql-server ado.net

有时会回滚

事务,即使代码没有错误。

当dbconnectin丢失或失败时,ado.net会回滚吗?它只会在特定的机器上不时发生。

enter image description here

  using (var dbTransactionManager = new DBTransactionManager())
        {
            try
            {

               //Insert statatement 1

               //Insert statatement 1 //after this statement, transaction is rollbacked, rest of the below statements run without transaction. this doesn't happen always.


               //Insert statatement 1

               //Insert statatement 1

               //Insert statatement 1




                dbTransactionManager.Commit();
                transactionClosed = true;
            }
            catch
            {
                //this never called.
                if (transactionClosed == false)
                {    
                    dbTransactionManager.RollBack();
                }

                throw;
            }
        }

1 个答案:

答案 0 :(得分:0)

是的,如果连接为空,则ADO.NET将执行回滚。正如jdweng在其评论中指出的那样,在事务的dispose方法中,将执行测试以检查是否connection == null。如果为null,将执行事务回滚。