4

We have a program that connects to our DB and run some Stored Procedures to get some data.

The database is a SQL Server 2008 and the program runs locally. The connection is via TCP/IP, but shared memory is enabled. And the timeout of the connection string is set to 45 sec.

When we run them through SQL Server Management Studio, it takes 0-5 seconds to run. When we run it through code, it randomly times out.

To make some test, we increased the timeout from 45sec to few minutes. And also to discard any blocking issue we checked that the Stored Procedures only "selects" data (with no insert or update statement). And we have tried several table modifiers for the select statements like: nolock, readpast, ...

Also I checked sp_who2 and dbcc opentran() and nothing is blocked... and the SPID of the .Net is running command .... For more details while the .Net is waiting a DB answer, through SQL Server Management Studio I can run the same statement (Stored Procedure or Select) without problems.

Any suggestion of what is happening?

indiPy
  • 7,844
  • 3
  • 28
  • 39
Alex
  • 797
  • 10
  • 30
  • Are you getting the SqlException on the call to SqlConnection.Open() or on the call that actually executes the query? – tomfanning Nov 09 '12 at 15:32
  • SqlException code returned is timeout. But the problem is the query seems is not executed by the SQL Server (despite `sp_who2` shows `running command`) – Alex Nov 09 '12 at 15:43
  • I didn't ask that. I asked what line of code throws the exception. This will help us to determine what is failing. – tomfanning Nov 09 '12 at 15:50
  • Can you include as much actual text from the exception message as possible please. There can be several different things that all say timeout. – Damien_The_Unbeliever Nov 09 '12 at 15:52
  • Please post the .NET and one problem SP – paparazzo Nov 09 '12 at 15:57

3 Answers3

5

The connection string timeout only influences the login timeout. You seems to hit a command timeout, and that can be changed only by modifying the CommandTimeout. The default value is 30 seconds, the recommended value is 0 (infinite timeout).

As for why your procedure hits random slow execution, I recommend to start by reading Slow in the Application, Fast in SSMS? Understanding Performance Mysteries

BTW, your query is likely not blocked. It executes a different plan that simply takes that long to execute. Checking for last_wait_type in sys.dm_exec_requests will likely reveal IO waits (PAGEIOLATCH, after to chasse any red-herring CXPACKET down the sys.dm_os_workers join...). But there is no point repeating the far more comprehensive and excellent article by Erland Sommarskog I originally linked.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • 6
    "... the recommended value is 0 (infinite timeout)." - Recommended by whom, and why? – Joe Nov 09 '12 at 15:44
  • 4
    If it were a universal recommendation to use 0, I'm sure that's what the default would be. – Damien_The_Unbeliever Nov 09 '12 at 15:52
  • Recommended by me, if that carries any weight for you. If queries would mean to time out, SSMS would time them out, sqlcmd would time them out, heck the engine itself would time them out. The default CommandTimeout value falls into the same bucket as the [default serialization level of the system transaction scopes](http://blogs.msdn.com/b/dbrowne/archive/2010/05/21/using-new-transactionscope-considered-harmful.aspx)... – Remus Rusanu Nov 09 '12 at 16:16
  • @RemusRusanu The reading you recomend is very interesting and I've learned a lot, but didn't solve this problem. – Alex Dec 08 '12 at 14:18
0

There are 2 kinds of timeout: Connection and Query.

If your query times out, it is a query problem ; but since you say you can run it thru SQL server management studio, I doubt it is query.

If your connection times out, it is most likely a network problem. I see that you have experimented (locks,hints etc..) but you are making an assumption that it is the query that is causing the issue. Try to think in terms of network. I have heard about activity monitors in SQL server which may help you detect network problems during connection.

Antony Thomas
  • 3,576
  • 2
  • 34
  • 40
0

if timeout is from .net side then add this 2 parameters to connection string.

Timeout=3600000;

Max Pool Size=360000;

Hiral
  • 465
  • 3
  • 12