Skip to content

Zombified transaction executes and auto-commits SQL command #4855

Description

@branko-d

Describe the bug

After certain high-severity errors (such as invalid type conversion or division by zero), the SQL Server transaction enters invalid ("zombified") state. However, a new SqlCommand can still be created from that transaction, executed and auto-committed, potentially leading to unwanted changes being saved to the database.

To reproduce

Create a simple console application, reference the "Microsoft.Data.SqlClient" and include the following code into Program.cs:

// Prerequisites...
//
// Create the following table in your database:
//
//      CREATE TABLE T(
//          X int PRIMARY KEY IDENTITY
//      );
//
// Adjust the `conn_string` below to point to your database.

using Microsoft.Data.SqlClient;
using System.Diagnostics;

var conn_string = new SqlConnectionStringBuilder() {
    DataSource = ".",
    InitialCatalog = "DB1",
    IntegratedSecurity = true,
    TrustServerCertificate = true
}.ConnectionString;

using var conn = new SqlConnection(conn_string);
conn.Open();

using var tran = conn.BeginTransaction();

Debug.Assert(tran.Connection is not null);

try {
    using (var cmd = new SqlCommand("SELECT CAST('n/a' AS int)", conn, tran)) {
        using (var reader = cmd.ExecuteReader()) {
            while (reader.Read())
                Console.WriteLine(reader.GetInt32(0));
        }
    }
}
catch (Exception ex) {
    Console.WriteLine(ex);
}

// The transaction is "zombified" at this point.
Debug.Assert(tran.Connection is null);

// Yet, we can still execute a command under it, and it auto-commits!
using (var cmd = new SqlCommand("INSERT INTO T DEFAULT VALUES", conn, tran)) {
    cmd.ExecuteNonQuery();
}

At this point, a new row exists in the table T.

The complete Visual Studio project is included as the attachment:

SqlZombifiedTransaction.zip

Expected behavior

I would expect to have some sort of exception when trying to use zombified SqlTransaction in any way, and it should definitely not create a completely new transaction that auto-commits. This is especially dangerous if the client ignores the exception and executes a new SqlCommand that writes to the database - which will be executed in its own (new!) transaction and auto-committed, completely independently from the SqlTransaction that was passed when that SqlCommand was created. This could lead to inadvertent modification/corruption of the data.

Further technical details

Microsoft.Data.SqlClient version: 7.1.1

.NET target: net10.0

SQL Server version: Microsoft SQL Server 2025 (RTM-CU9-GDR) (KB5126967) - 17.0.5015.4 (X64) Oct 2 2026 17:36:09 Copyright (C) 2025 Microsoft Corporation Standard Developer Edition (64-bit) on Windows 10 Pro 10.0 (Build 26300: ) (Hypervisor)

Operating system: Windows 11 Pro

Activity

  1. added theissue type on Oct 17, 2026
  2. moved this from To triage to In progress in SqlClient Boardon Oct 18, 2026
  3. sanjaysingh commented on Oct 18, 2026

    @sanjaysingh

    Reproduced this on SQL Server 2022. After the failed cast, the insert on that transaction was committed. Here is a PR that addresses it: #4857

  4. cheenamalhotra commented on Oct 22, 2026

    @cheenamalhotra
    Member

    I verified the repro with SQL Server Profiler traces and the claim is correct. The query after a rollbacked transaction gets execution in implicit transaction mode, which shouldn't happen.

    Thanks for submitting a fix @sanjaysingh - I think it's worth backporting as well once validated.

    cc @priyankatiwari08 @paulmedynski

  5. moved this from In progress to In review in SqlClient Boardon Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions