Ever seen these two errors on ASP.Net application:
1) “Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding”.
2) “Request timed out”.
These errors are in a way related. There are two timeout thresholds available in the life cycle of an ASP.Net request. First: The amount of time taken to execute an SQL Query, Second: The overall time taken to complete the request. Error conditions can happen if your application takes more time on any of the two operations.
Overall Request Time Span (default 110 seconds)
= Time spent prior to SQL execution
+ Time spent during SQL execution (default 30 seconds)
+ Time spent after SQL execution
For a snappy application, ideally each of the above components should be completed in few 100 milliseconds, with overall time spent taking less than a second.
However, in poorly designed applications, any of the segments can take several seconds to complete. For example, what if your SQL execution takes more than 30 seconds. In this case, your command object will throw “Timeout Expired” exception. Ideally, you should wrap try catch around your SQLs or better the business layer invoking those data calls and should deal with the errors.
Besides SQL Timeouts, an application can spend time doing other work, for e.g. waiting for a web service to respond or doing complex calculations, reading a large file, in which case the overall time allocated to execute and complete the request may exceed the default timeout (110 seconds). If that happens ASP.Net will yank that request and return a response “Request Timed Out” response back to the client (browser).
Solution:
1) In order to fix “Timeout expired” issue, you can need to bump the SqlCommand.CommandTimeout property from default of 30 seconds to something as desired by your application.
Unfortunately, there is no easy way to configure that using web.config connection string. You should probably create a new setting in web.config for CommandTimeOut and use that in your data layer while executing the actual SQL commands.
2) In order to fix “Request Timed Out” problem, you can simply edit the web.config file and bump the httpRuntime.executionTimeOut property. The default value is 110 seconds:
<httpRuntime executionTimeout=”200″/>
Caveat:
As I mentioned earlier, these two errors are related. For example, if you set the CommandTimeOut to be 240 seconds, and executionTimeout to be 180 seconds, and query takes roughly 200 seconds to execute. In this case, the executionTimeout threshold will reach first, thereby causing ASP.Net to yank that request, even though the CommandTimeOut has not expired yet.
A general rule of thumb: ExecutionTimeOut should be equal to or preferably more than CommandTimeOut.
Summary:
SqlCommand.CommandTimeout and httpRuntime.executionTimeOut can be used to fix two common ASP.Net timeout issues. However, I personally think web applications should be snappy, and ideally one should not bother with any of the two settings.
If you are reading this blog and trying to use the recommendations, I’d strongly recommend to step back and take another look into the app and fix the real problem, you are just treating the symptom, not the root cause.