Geoffrey McGrath
Geoffrey McGrath

Reputation: 1673

When SQL_ATTR_QUERY_TIMEOUT is exceeded, does SQL Server stop work?

When calling SQL Server from a client using ODBC, if a long-running query is run causing the time specified in SQL_ATTR_QUERY_TIMEOUT to be exceeded, I see that control is returned to the application. My question is does the work continue within the SQL Server engine. If it does continue what can be done to abort/cancel/stop the request on the server? Is there a best practice consideration to keep in mind?

Upvotes: 0

Views: 917

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294277

The client sends an Attention signal to the server:

The client can interrupt and cancel the current request by sending an Attention message. This is also known as out-of-band data, but any TDS packet that is currently being sent MUST be finished before sending the Attention message. After the client sends an Attention message, the client MUST read until it receives an Attention acknowledgment.

The engine will abort the batch at the first opportunity (for all practical reasons, right away) and send back the Attention ack. In certains states a batch cannot be interrupted, eg. while rolling back a transaction. In such case a client may request an abort, but the response will come only after the server terminates the non-interruptible work.

The above is true for any SQL Server client stack, eg. exactly the same is how SqlCommand.CommandTimeout works.

The KILL command works in a very similar manner except that is not a client-server communication, but killing-SPID -> victim-SPID communication.

Upvotes: 2

Related Questions