Data committed even though System.Transactions.TransactionScope.Commit() not called

前端 未结 4 1735
温柔的废话
温柔的废话 2020-12-19 12:39

Under what circumstances can code wrapped in a System.Transactions.TransactionScope still commit, even though an exception was thrown and the outermost scope ne

相关标签:
4条回答
  • 2020-12-19 12:56

    This example (C#, .NetFramework 4.7.1) shows how we can persist to the database even if the code is wrapped in a TransactionScope. The first insert will be rolled back, and the second insert will be inserted without transaction.

    See my related post, where I ask for help in how to detect this.

    
    using (var transactionScope = new TransactionScope())
    {
        using (var connection = new SqlConnection("Server=localhost;Database=TestDB;Trusted_Connection=True"))
        {
            connection.Open();
    
            new SqlCommand($"INSERT INTO TestTable VALUES('This will be rolled back later')", connection).ExecuteNonQuery();
    
            var someNestedTransaction = connection.BeginTransaction();
            someNestedTransaction.Rollback();
    
            new SqlCommand($"INSERT INTO TestTable VALUES('This is not in a transaction, and will be committed')", connection).ExecuteNonQuery();
        }
    
        throw new Exception("Exiting.");
    
        transactionScope.Complete();
    }
    
    
    0 讨论(0)
  • 2020-12-19 13:01

    The answer turned out to be because I was creating the TransactionScope object after the SqlConnection object.

    I moved from this:

    using (new ConnectionScope())
    using (var transaction = new TransactionScope())
    {
        // Do something that modifies data
    
        transaction.Complete();
    }
    

    to this:

    using (var transaction = new TransactionScope())
    using (new ConnectionScope())
    {
        // Do something that modifies data
    
        transaction.Complete();
    }
    

    and now it works!

    So the moral of the story is to create your TransactionScope first.

    0 讨论(0)
  • 2020-12-19 13:10

    The obvious scenario would be where a new (RequiresNew) / null (Suppress) transaction is explicitly specified - but there is also a timeout/unbinding glitch that can cause connections to miss the transaction. See this earlier post (the fix is just a connection-string change), or full details.

    0 讨论(0)
  • 2020-12-19 13:18

    Be aware how TransactionScope works:
    It sets property System.Transactions.Transaction.Current at the begining of using scope and then set it back to previous value at end of using scope.

    Previous value depends on where given scope is declared. It can be inside another scope.


    You can modify code like this:

    using (var sqlConnection = new ConnectionScope())
    using (var transaction = new TransactionScope())
    {
        sqlConnection.EnlistTransaction(System.Transactions.Transaction.Current);
        // Do something that modifies data
        transaction.Complete();
    }
    

    I show this possibility for those who have their code more complicated and cannot simply change code to open DB connection first.

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