SQL Server – Understanding Connection Timeouts and Query Timeouts

Have you ever got into a situation wherein you feel you see something and work for a solution in a completely opposite direction? Recently I was out of the country for a session and at the breakfast table I found a dark red bottle of ketchup which I used for my sandwich. Lesser did I know it was actually a spicy sauce. And on closer look this time, I found the mistake I did and how I ruined my whole breakfast. These are times when you oversee something which looks like one and you land up as something completely different. This blog post we will be Understanding Connection Timeouts and Query Timeouts.

SQL Server - Understanding Connection Timeouts and Query Timeouts connection-800x565

During consultancy there are times when you are as an open book of all kinds of questions. When I was at a customer location, I was confronted by something simple. The developer walked up to me and said he had a question related to SQL Server. Eager to hear, I asked the problem statement. He said he wanted to show me a code block.

System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

Now this is one of the generic error which you can ever get. I was trying to explain him how the errors and settings are when working with such error. I wanted to look at the time when the error comes. At this moment you need to understand the reason. Let us look at what the defaults are for our timeouts to put some context around this. These numbers can be adjusted in different ways. Either through code, or a connection string for example.

Connection Timeout setting: 15 Seconds

Command/Query Timeout Setting: 30 Seconds

Interestingly, with this rule of thumb and since he was using the default – I could guess he was getting Query Timeout in his code. I went on to talk how being on the latest and greatest would have easily solved him the problem. He was not sure what I meant. Since he was on .NET 2.0, we both walked to another machine with .NET 4.5 installed and I we wrote a simple code to see how it would work. We tried to get a stack dump too, as part of it. Now keeping the specifics apart, we got:

System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed while …

And when we encountered Query Timeout it read like:

System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior …

Sometimes we see such interesting arguments on why upgrading is better or not. I was with my limited knowledge easily know how he can get productivity as a developer by moving to the latest and greatest of the platform. I know I have limited knowledge on .NET but this is something I remember seeing in one of the sessions at the Bangalore User Group by the connectivity engineer. This stuck in my mind for a long time and it came handy big time.

After I found his problem statement. I was delighted. I told him not to change any of the default settings. Since his problem was a query timeout, I wanted to look at the query that was running behind and why it was taking more than normal acceptable time on his system. This was a great way for me to solve something in the application and later start tuning the SQL Server in their environment.

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL Connection, SQL Server
Previous Post
PowerShell – Create a User with Read Rights on a Database
Next Post
SQL SERVER Management Studio – Why I am Seeing White Icon Instead of Green or Red?

Related Posts

5 Comments. Leave new

  • We are using Always On Availability Group feature of SQL Server 2012. Regular full database backups and transaction log backups are done every day on the Primary database. But, we observed Transaction log growing rapidly and now it reaches equal to our database size. Can you guide how can we truncate transaction log, if Always on feature is used.

  • ” I found a dark red bottle of ketchup which I used for my sandwich….I ruined my whole breakfast. ”

    Ketchup on a sandwich? A sandwich for breakfast?

  • I was facing an timeout issue last week, we used execution plan in Sql Server to get the query analysis. But we acted wrongly on that analysis and tried putting indexes on tables, but after digging more we found out that the issue was in query itself as it was pulling a huge amount of data.

  • Meenakshi Agarwal
    October 24, 2016 12:03 am

    Good explanation of connection and query timeouts. Also, examples given here helped in clearing a few doubts.

  • Carlos Bautista
    June 18, 2021 1:11 pm

    When I run a procedure from a .NET API it sends me time out, but when I run the SP from SQL server it returns a result.


Leave a Reply