mkus
mkus

Reputation: 3487

How does ADO.NET' s SqlCommand.CommandTimeout work?

Consider a stored procedure that updates some rows about in 60 seconds without using a transaction. We set ADO.NET's SqlCommand.Timeout to 30 seconds.

SqlCommand.Timeout = 30;

When that timeout occurs at 30 seconds, will the stored procedure continue to run in database server or not? How does the server communicate this to the client?

Upvotes: 3

Views: 4002

Answers (3)

Leo Muller
Leo Muller

Reputation: 1483

I was wondering about the exact same. I could not find an answer, but did some experimenting, and it looks like some kind of cancel query is send:

  • If there is a trigger involved or the table is locked, it won't update.
  • if there are multiple statements, not in a transaction, it will cancel the ones after the timeout occured. What was already done stays done.
  • If you add a delay before your insert, nothing will be in your table.
  • if you add a delay after your insert, the insert will be written to your table.

I know this is a five year old post, but if I got here, others will too :-)

By the way, in ado.net, in order to increase your timeout, you must increase in both the sqlcommand and in the sqlconnection, and the defaults for the first is 15 sec, the second 30 sec. So if you just change the sqlcommand to 60 secs, it will still timeout after 30 sec, which can be rather puzzling.

Upvotes: 2

Tahbaza
Tahbaza

Reputation: 9548

The answer is no, your attempted action on the server will fail after 30s, your SqlCommand object will throw an exception in your code (below) and the implicit stored procedure transaction will rollback.

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

...at least this is the behavior that I can verify using SQL Server...

Upvotes: 7

Cahit
Cahit

Reputation: 2534

If you haven't closed your SQL connection yet, it should continue to run if it started proc execution already. (You should be able to test & verify this relatively easily.)

Upvotes: 0

Related Questions