2

I am writing an application which updates data in separate but related database tables. I would like to perform the update queries within a transaction from within my code. I know of two potential ways of doing this:

My question is: will either of these models result in the transactions being rolled back if the process is terminated in the middle, i.e. if someone literally kills the process forcefully? For example, if I have this code which uses the first model and the process is terminated between the two commands, will the transaction be rolled back?

try
{
    using (var conn = new SqlConnection(connectionString))
    {
        using (var tran = conn.BeginTransaction("MyTran"))
        {
            using (var firstCommand = new SqlCommand(firstQuery, conn))
            {
                firstCommand.ExecuteNonQuery();
            }

            //PROCESS IS TERMINATED HERE.

            using (var secondCommand = new SqlCommand(secondQuery, conn))
            {
                secondCommand.ExecuteNonQuery();
            }

            tran.Commit();
        }
    }
}
catch (Exception)
{
    //do whatever.
}

Or if I have the following code which uses the second model and the process is terminated between the two method calls, will the transaction be rolled back?

try
{
    using (var scope = new TransactionScope())
    {
        MyFirstUpdate();
        //PROCESS IS TERMINATED HERE.
        MySecondUpdate();
        scope.Complete();
    }
}
catch (Exception)
{
   //do whatever.
}

I have not been able to find any information in MSDN or elsewhere which indicates what the results will be in either of these cases.

2 Answers 2

4

If a transaction is not commited, then it is rolled back. Killing a process prevents the COMMIT to be applied as the TransactionScope.Complete is never called, and terminates the connection which will cause a rollback.

2
  • Okay so the connection doesn't need to be "gracefully" terminated, i.e. the server-side will take care of that in cases where the connection is suddenly orphaned?
    – rory.ap
    Apr 14, 2015 at 15:58
  • Exactly yes. The database engine knows the connection has gone away and rolls back anything it has to.
    – DavidG
    Apr 14, 2015 at 15:59
2

If we're talking about abruptly terminating a process, none of the "rollback" logic gets a chance to run and no messages are sent to the server. This is different from general exception-handling, where finally blocks will usually ensure rollbacks occur.

The behaviour of SQL Server is to apply a timeout to connections - if the connection doesn't send a keep-alive message before the timeout it is considered dead and is closed at the SQL Server end. If there is a transaction pending, it will be rolled back when the associated connection is closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.