Repository navigation
Description
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:
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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn review
Activity
sanjaysingh commentedon Oct 18, 2026
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
cheenamalhotra commentedon Oct 22, 2026
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