Blog Post

Transactions: Rolling back a nested transaction

,

Transactions are great and wonderful things. They make sure that our work stays atomic, consistent, isolated and durable (yes ACID). And if we use an explicit transaction (created by BEGIN TRANSACTION) and we make a mistake we can always roll the transaction back. Unfortunately rolling back a transaction and committing a transaction aren’t quite the same.

Transactions can be nested. We can tell how many layers of transactions deep we are by looking at @@TRANCOUNT. When we create an explicit transaction it increases @@TRANCOUNT by 1. When we commit a transaction it decreases @@TRANCOUNT by 1. However when we roll back a transaction it decreases @@TRANCOUNT to 0. This means no matter how deep in nested transactions we are one ROLLBACK will undo the whole thing all the way back to the top. Easy to say but in order to help it sink in how about an example or two?

-- Create a table to use during the tests
CREATE TABLE tb_TransactionTest (value int)
GO
-- Test using 2 transactions and a rollback on the 
-- outer transaction
BEGIN TRANSACTION -- outer transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (1)
BEGIN TRANSACTION -- inner transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (2)
COMMIT -- commit the inner transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (3)
ROLLBACK -- roll back the outer transaction
PRINT @@TRANCOUNT
SELECT * FROM tb_TransactionTest
GO

This demonstrates committing an “inner” transaction then rolling back the “outer”. Even though the “inner” transaction is committed when the “outer” one is rolled back the whole thing gets rolled back and no rows are in the table.

Next let’s go the other way around.

-- Test using 2 transactions and a rollback on the 
-- inner transaction
BEGIN TRANSACTION -- outer transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (1)
BEGIN TRANSACTION -- inner transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (2)
ROLLBACK -- roll back the inner transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (3)
-- We get an error here because there is no transaction
-- to commit.
COMMIT -- commit the outer transaction
PRINT @@TRANCOUNT
SELECT * FROM tb_TransactionTest
GO

After rolling back the inner transaction we actually get an error when trying to commit the outer transaction. Logically it might seem like you could roll back the inner transaction and commit the remainder of the transaction however it doesn’t work that way. Once the ROLLBACK command is executed the whole transaction is rolled back all the way to the beginning. The last insert statement (value 3) is in an implicit transaction and will commit even though the COMMIT statement returns an error. So we end up with one row with a 3 in it.

One way to avoid the error is to take advantage of the @@TRANCOUNT system variable.

-- Test using @@TRANCOUNT to avoid an error when we
-- used a ROLLBACK. 
-- Clean up from last time
TRUNCATE TABLE tb_TransactionTest
BEGIN TRANSACTION -- outer transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (1)
BEGIN TRANSACTION -- inner transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (2)
ROLLBACK --roll back the inner transaction
PRINT @@TRANCOUNT
INSERT INTO tb_TransactionTest VALUES (3)
IF @@TRANCOUNT > 0
-- No error this time
COMMIT -- commit the outer transaction
PRINT @@TRANCOUNT
SELECT * FROM tb_TransactionTest
GO

Transactions are a big subject which I’m going to explore over several posts. I am by no means going to cover the subject exhaustively but if you have any subjects you would like me to cover or think I’ve missed something feel free to comment or email me.

Filed under: Microsoft SQL Server, SQLServerPedia Syndication, T-SQL, Transactions Tagged: code language, language sql, microsoft sql server, T-SQL, transactions

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating