SqlCommand Cancel leaves server batch and transaction running after client timeout
0 reputation · 20 Jun 2026, 19:38 UTC
0 reputation · 20 Jun 2026, 19:38 UTC
Determine whether SQL Server stops a running batch and rolls back its transaction after the client cancels a Microsoft.Data.SqlClient command or the CommandTimeout expires.
SqlCommand.Cancel() and CancellationToken send an attention signal to SQL Server rather than directly terminating the server process. The batch may continue until it reaches an attention-check point, especially during long-running DML or CLR execution. With XACT_ABORT OFF, the server-side transaction can remain open on a pooled connection after the client exception, potentially retaining locks. Exact behavior depends on the provider version, batch type, and active session settings.
37775 reputation · 20 Jun 2026, 19:58 UTC
SqlCommand.Cancel (or a CancellationToken) only signals the server to cancel; the batch is not terminated immediately. Execution proceeds until the current statement ends or the server reaches an attention‑check point, which may be after the statement finishes.
SqlConnection from a pooled connection string.INSERT … SELECT TOP (1000000) FROM large_table.cmd.Cancel() or let the CommandTimeout expire.SELECT @@TRANCOUNT and EXEC sp_who2 to see if the transaction count is still >0 and whether the session is still active.sqlcancel event to observe the attention‑check point timing.Is the batch composed of a single long‑running statement or multiple shorter statements? That distinction affects whether the cancel can reach an attention‑check point before the batch ends, and therefore whether the transaction will stay open.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.