Azure SQL Database 2022: Query Timeout Enforcement Across Multi-Statement Batches and Distributed Transactions
0 reputation · 01 Jan 2020, 07:04 UTC
0 reputation · 01 Jan 2020, 07:04 UTC
Understanding how client-configured query timeouts and explicit cancellation calls propagate through Azure SQL Database when a workload spans multiple statements in a single batch or participates in a distributed transaction.
Documentation indicates that a client-side timeout (for example, CommandTimeout or setQueryTimeout) is enforced per statement, yet the behavior for the surrounding transaction is not guaranteed. Error codes such as 40613 and 60001 may surface after partial row processing, leaving the transaction in an ambiguous committed or uncommitted state. Driver version differences further complicate whether the timeout reaches the server at all. In geo-replicated or high-availability configurations, a cancelled query can continue executing on secondary replicas until the primary acknowledges the cancellation, delaying cleanup.
SqlCommand.Cancel() is invoked inside a distributed transaction, what determines whether the transaction rolls back automatically versus remaining open?34775 reputation · 01 Jan 2020, 15:54 UTC
In Azure SQL Database, a client-configured timeout (such as SqlCommand.CommandTimeout) does not act as a server-side governor but as a client-side timer. When this timer expires, the driver sends an attention signal to the server to cancel the current execution.
A client-set timeout aborts the entire batch at the point of expiration. It does not skip the current statement to move to the next; rather, it terminates the execution of the remaining statements in that specific batch. However, the timeout itself does not trigger an automatic transaction rollback unless SET XACT_ABORT ON is configured. If XACT_ABORT is OFF (the default), statements executed prior to the timeout within the same transaction may remain uncommitted but active, potentially holding locks until the session expires or a manual rollback is issued.
When SqlCommand.Cancel() is invoked within a distributed transaction, the following logic applies:
Transaction.Rollback() or if the transaction coordinator (e.g., MSDTC or the Azure SQL elastic transaction coordinator) determines the participant is unreachable or has failed.This behavior assumes the use of modern ADO.NET or JDBC drivers. Older drivers may handle attention signals inconsistently, occasionally failing to notify the server of a timeout until the connection is physically dropped.
To verify the state of your transactions during a timeout, use the following scoped query to identify orphaned sessions or open transactions:
SELECT session_id, open_transaction_count, status
FROM sys.dm_exec_sessions
WHERE open_transaction_count > 0;
Required Diagnostic Detail: To provide a more specific recommendation on rollback behavior, please specify if you are using SET XACT_ABORT ON in your session initialization scripts.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.