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

前端 未结 3 1638
甜味超标
甜味超标 2021-01-22 10:47

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.

SqlComma         


        
相关标签:
3条回答
  • 2021-01-22 11:03

    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.

    0 讨论(0)
  • 2021-01-22 11:14

    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.)

    0 讨论(0)
  • 2021-01-22 11:26

    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...

    0 讨论(0)
提交回复
热议问题