Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is zombie transaction?

I accidentally found zombie transaction is mentioned in SqlTransaction code. So, what is zombie transaction?

like image 885
Andrew Bezzub Avatar asked Oct 28 '10 13:10

Andrew Bezzub


1 Answers

A zombie transaction is a transaction that cannot be committed (due to an unrecoverable error) but is still open.

CREATE TABLE mytable (id INT NOT NULL PRIMARY KEY)
SET XACT_ABORT ON;
BEGIN TRY
        BEGIN TRANSACTION
                INSERT
                INTO    mytable
                VALUES  (1)
                INSERT
                INTO    mytable
                VALUES  (1)
        COMMIT
END TRY
BEGIN CATCH
        PRINT XACT_STATE()
        SELECT  *
        FROM    mytable
        ROLLBACK;
END CATCH
SELECT  *
FROM    mytable

Here, the second INSERT renders the transaction zombie.

It cannot write anymore and should be rolled back, but you can still read in its scope (the innermost SELECT returns a record; the outermost does not).

like image 142
Quassnoi Avatar answered Oct 22 '22 08:10

Quassnoi