Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout not being honoured in connection string

Tags:

I have a long running SQL statement that I want to run, and no matter what I put in the "timeout=" clause of my connection string, it always seems to end after 30 seconds.

I'm just using SqlHelper.ExecuteNonQuery() to execute it, and letting it take care of opening connections, etc.

Is there something else that could be overriding my timeout, or causing sql server to ignore it? I have run profiler over the query, and the trace doesn't look any different when I run it in management studio, versus in my code.

Management studio completes the query in roughly a minute, but even with a timeout set to 300, or 30000, my code still times out after 30 seconds.

like image 376
David Wengier Avatar asked Aug 26 '08 06:08

David Wengier


People also ask

Can I set command timeout in connection string?

You have always been able to specify the Connect Timeout via the SqlClient connection string, but as documented, this applies to establishing a connection with the database server, not executing commands / running queries (meaning running one of the SqlDataReader ExecuteXxx methods).

What is connection timeout in connection string?

The time (in seconds) to wait for a connection to open. The default value is 15 seconds.

How do I set SQL Server connection timeout?

Using SQL Server Management StudioIn Object Explorer, right-click a server and select Properties. Click the Connections node. Under Remote server connections, in the Remote query timeout box, type or select a value from 0 through 2,147,483,647 to set the maximum number seconds for SQL Server to wait before timing out.


2 Answers

What are you using to set the timeout in your connection string? From memory that's "ConnectionTimeout" and only affects the time it takes to actually connect to the server.

Each individual command has a separate "CommandTimeout" which would be what you're looking for. Not sure how SqlHelper implements that though.

like image 178
Matt Hamilton Avatar answered Oct 20 '22 10:10

Matt Hamilton


In addition to timeout in connection string, try using the timeout property of the SQL command. Below is a C# sample, using the SqlCommand class. Its equivalent should be applicable to what you are using.

SqlCommand command = new SqlCommand(sqlQuery, _Database.Connection); command.CommandTimeout = 0; int rows = command.ExecuteNonQuery(); 
like image 22
Ishmaeel Avatar answered Oct 20 '22 11:10

Ishmaeel