如果 TransactionScope.Complete 在 Using 块的末尾未调用,Transaction 会发生什么

What happens to Transaction if TransactionScope.Complete not called at the end of the Using block

我正在使用 SQL Server 2012 并且在 TransactionScope Using 块中有多个 SQL 连接到同一个数据库。但是,如果第一次更新 SQL 操作没有产生所需的输出,那么我将跳过对 SQL 操作的下一次调用,并且还会调用 TransactionScope.Complete 未在 Using 块末尾调用.

未能调用事务完成将导致事务中止,这就是调用 transaction.complete();[= 是一个 非常好的参数 的原因14=]

事务管理器会将其解释为系统故障并抛出异常(发生在我身上一次,不太好),然而这是一个很大的然而,它不保证事务会被提交,所以你也必须调用提交。

例如:

       using (TransactionScope scope = new TransactionScope())
        {
            using (SqlConnection connection1 = new SqlConnection(connectString1))
            {
                // Opening the connection automatically enlists it in the 
                // TransactionScope as a lightweight transaction.
                connection1.Open();

                // Create the SqlCommand object and execute the first command.
                SqlCommand command1 = new SqlCommand(commandText1, connection1);
                returnValue = command1.ExecuteNonQuery();
                writer.WriteLine("Rows to be affected by command1: {0}", returnValue);

                // If you get here, this means that command1 succeeded. By nesting
                // the using block for connection2 inside that of connection1, you
                // conserve server and network resources as connection2 is opened
                // only when there is a chance that the transaction can commit.   
                using (SqlConnection connection2 = new SqlConnection(connectString2))
                {
                    // The transaction is escalated to a full distributed
                    // transaction when connection2 is opened.
                    connection2.Open();

                    // Execute the second command in the second database.
                    returnValue = 0;
                    SqlCommand command2 = new SqlCommand(commandText2, connection2);
                    returnValue = command2.ExecuteNonQuery();
                    writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
                }
            }

            // The Complete method commits the transaction. If an exception has been thrown,
            // Complete is not  called and the transaction is rolled back.
            scope.Complete();

        }

提交将发生在 using 块的末尾,即 如果 TransactionScope 对象最初创建了工作。否则,只要调用 commit 行 .

,提交就会发生

您需要的大部分信息都在这里得到了很好的安排Completing a transaction scope

When your application completes all the work it wants to perform in a transaction, you should call the Complete method only once to inform the transaction manager that it is acceptable to commit the transaction. It is very good practice to put the call to Complete as the last statement in the using block.

Failing to call this method aborts the transaction, because the transaction manager interprets this as a system failure, or equivalent to an exception thrown within the scope of the transaction. However, calling this method does not guarantee that the transaction wil be committed. It is merely a way of informing the transaction manager of your status. After calling the Complete method, you can no longer access the ambient transaction by using the Current property, and attempting to do so will result in an exception being thrown.