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