5

I am using the SQL Server 2012 and There are multiple SQL Connections to same database inside the TransactionScope Using block. But if first Update SQL Operation does not result desired output then I am skipping next call to SQL Operation and also call to TransactionScope.Complete not called at the end of the Using block.

Bhalchandra K
  • 2,631
  • 3
  • 31
  • 43
  • 1
    https://learn.microsoft.com/en-us/dotnet/framework/data/transactions/implementing-an-implicit-transaction-using-transaction-scope#completing-a-transaction-scope – TheGeneral Aug 27 '18 at 05:15
  • Thank you @TheGeneral . Can you post as an anwser, so that i can accept it. – Bhalchandra K Aug 27 '18 at 05:17
  • Possible duplicate of [Will an inner transaction scope roll back if the outer transaction scope doesn't complete?](https://stackoverflow.com/questions/4497910/will-an-inner-transaction-scope-roll-back-if-the-outer-transaction-scope-doesnt) – Emre Savcı Aug 27 '18 at 05:19

2 Answers2

4

Most of the information you need is fairly well laid out here 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.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
2

Failing to call the transaction complete will cause in the abortion of the transaction, that is the reason why it is a a very good parcitce to call transaction.complete();

What will happen is that the transaction manager will interpret it as system failure and will throw exceptions (happened to me once, not nice), however and it is a big however, it does not promise the transaction will be committed so you will have to call commit as well.

for example:

       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();

        }

the commit will happen at the end of the using block, that is if the TransactionScope object created the work initially. Else, the commit will occur whenever the commit line was called.

Barr J
  • 10,636
  • 1
  • 28
  • 46