如何检测到发生回滚?

时间:2019-09-02 21:13:19

标签: c# sql-server msdtc

我正在寻找一个大型业务应用程序中的一个错误,该应用程序中的业务流程虽然失败了,但是部分保存在数据库中。为了使问题更难弄清,该过程每隔几周就会失败一次,每次失败之间都会成功处理数十万次。当并发性/工作进程数增加时,错误频率似乎会增加。

到目前为止,我们已经能够重新创建该程序所看到的内容。 (.NET 4.7.1框架)

using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    using (var sqlConnection = new SqlConnection("Server=localhost;Database=Database1;Trusted_Connection=True"))
    {
        sqlConnection.Open();

        // Doing some unwanted nested manual transaction and rolling it back
        var transaction = sqlConnection.BeginTransaction();
        new SqlCommand($"INSERT INTO TestTable VALUES('This will be rolled back}')", sqlConnection).ExecuteNonQuery();

        transaction.Rollback();

        // Now doing some work with the DB.
        // You might expect it to become a part of transactionScope, but that is NOT the case!
        // So this command will actually succeed, with data written to the DB.
        new SqlCommand($"INSERT INTO TestTable VALUES('This will be inserted')", sqlConnection).ExecuteNonQuery();

        // Doing something which promotes the local transaction to a distributed transaction.
        using (var sqlConnection2 = new SqlConnection("Server=localhost;Database=Database2;Trusted_Connection=True"))
        {
            // The program will fail here, with message "The transaction has aborted, Failure while attempting to promote transaction."
            sqlConnection2.Open();

            new SqlCommand($"INSERT INTO TestTable2 VALUES('We will never come this far')", sqlConnection2).ExecuteNonQuery();

        }
    }
    transactionScope.Complete();
}

我们的生产代码没有显式调用transaction.Rollback(),在我的示例中,这只是重现错误消息和行为的方式。但是,如果我们的任何第三方库都发出了此调用,我想抛出异常并尽快退出。最好在应用层。

如何检测到已对Rollback()进行了呼叫?我真的不想在不确定transactionScope能够完成任务的情况下进行粗暴的操作。

更新1

我不需要的“回滚”是由.Net的连接共享机制中的某个错误引起的。该错误在4.5.1和4.8之间的所有.Net Framework版本以及新的System.Data.SqlClient package上都已复制。

一个issue has been added to the System.Data.SqlClient repository

1 个答案:

答案 0 :(得分:2)

不同的事务API不能一起使用。所以你在这里很危险。

  

如何检测到对Rollback()的调用?

select @@trancount应该始终告诉您。回滚会将@@ trancount还原为0。