Here is a way to detect Zombie transaction
SqlTransaction trans = connection.BeginTransaction();
//some db calls here
if (trans.Connection != null) //Detecting zombie transaction
{
trans.Commit();
}
Decompiling the SqlTransaction class, you will see the following
public SqlConnection Connection
{
get
{
if (this.IsZombied)
return (SqlConnection) null;
return this._connection;
}
}
I notice if the connection is closed, the transOP will become zombie, thus cannot Commit
.
For my case, it is because I have the Commit()
inside a finally
block, while the connection was in the try
block. This arrangement is causing the connection to be disposed and garbage collected. The solution was to put Commit
inside the try
block instead.