Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Proc and SqlCommand Timeout

If I run a stored proc using the SqlCommand and the SqlCommand times out does the StoredProc continue to execute or does it get force to quit when the SqlCommand disconnects?

like image 707
RoboDev Avatar asked Nov 11 '09 14:11

RoboDev


People also ask

What is the default timeout for Sqlcommand class?

Gets or sets the wait time (in seconds) before terminating the attempt to execute a command and generating an error. The default is 30 seconds.

How do I fix SQL timeout error?

Troubleshoot timeout expired errors If you encounter a connection-timeout error, follow the steps: Increase the connection-timeout parameter. If you use an application to connect to SQL Server, increase the relevant connection-timeout parameter values and check whether the connection eventually succeeds.

What is the difference between connection timeout and command timeout?

CommandTimeout is how long a single command can take to complete. ConnectionTimeout is how long it can take to establish a connection to the server to start with.

Is CommandTimeout in seconds or milliseconds?

It seems that people are confused as to whether this is seconds or milliseconds. The documentation states that the timeout is in seconds. A 1-minute timeout seems reasonable for most queries.


1 Answers

My instinct said that the procedure would still execute so I threw together a simple test.

SQL:

Create Procedure TestDelay
AS

waitfor delay '00:00:40'

update table_1
set dt = getdate()

And in VB.Net (same as C# for this purpose):

    Dim con As New SqlConnection(myconnectionstring)
    Dim com As New SqlCommand("TestDelay", con)
    com.CommandType = CommandType.StoredProcedure
    con.Open()
    Try
        com.ExecuteNonQuery()
    Catch ex As Exception
        con.Close()
        Response.Write(ex.Message)
    End Try

The result? The procedure did not complete after the timeout. I checked what was happening during a trace in SQL profiler and sure enough SQL appears to wrap the call in a transaction and must roll that transaction back on the timeout.

Note: This test was run against SQL 2005 but I'd suspect the results to be similar in other versions.

like image 181
brendan Avatar answered Oct 04 '22 23:10

brendan