Inexplicable query timeout with ASP.NET and SQL Server - asp.net

Sometimes, my web application is throwing timeout exceptions when trying to execute a specific stored procedure. From that moment on, the stored procedure will never execute again until I reboot the database server.
The strange thing is that I can execute the stored procedure manually from within the SQL Server Management studio, and the execution time is correct (about 0.2 seconds).
But if the same exact call is made from the webserver... Timeout. How is that possible?
I am using SQL Server 2012 and I'm mapping the stored procedure in my code using Linq2Sql.
Additional information: I have tried running the "detect blocking" sql from this blog post: http://blog.sqlauthority.com/2010/10/06/sql-server-quickest-way-to-identify-blocking-query-and-resolution-dirty-solution/ but no rows are returned.

Possibly a bad query plan is cached in SQL server. You could try to recompile the Stored Procedure with the Recompile option. With this option the SP is not cached and is recompiled every time it is called.

Related

What happens with SQL operation when IIS Times out in ASP.NET Http Request?

My ASP.NET application is making a database call (stored procedure) to fetch data from SQL server. The number of records to process is large and operation is going to take long time. IIS times out the request after default executionTimeout (110 seconds) which is completely fine in my case. My real question is what happens to the query that is running on the SQL server? Does IIS send any command like abort to sql server to stop the query execution?
Command timeout (300 seconds) set on the stored procedure call from ADO.NET is way more than http request execution timeout (110 seconds).
I found this and this but they do not have the answer what I'm looking for.
This article explains about how a client sends an abort command to server after command timeout has reached.
The client here is asp.net application in my case. So, IIS has to notify asp.net application that request has reached it max time and then asp.net application signals sql server to abort the executing query. Is that how it works or Am I totally understanding this topic wrong?
This is what I have tried so far running locally using Visual studio 2017 and IIS express: Set executionTimeout="30" and debug = "false" in web.config file. In stored procedure I set WAITFOR DELAY '00:05:00' for the query to run longer than http request time out. But the web request never timed out. It is waiting for the query to complete.

Asp.net and Mssql Database operations time out and rollback

I am developing an application that has a big database and it waits a bit long for the result from a few queries. Sometimes, users refreshes interface before the execution is finished. There are ten users and this scenario occurs so many times.I think scripts do not stop running on sql when the web page is refreshed (becuase performance decrasing so much after a few hours). If so, Can I stop the execution of the script when the page is refreshed. In web.config, will changing Connect Timeout attibute stop the execution and rollback operations on database after timeout period is passed? Or Is there any other option in web.config file?
No connection timeout property is meant to set the timeout after which the attempt to connect to the SQL server fails. What you need is a command timeout for your stored procedure. If you share your code, expect more help.

SQL connection timeout

We have been facing weird connection timeouts on one of our websites.
Our environment is composed of an IIS 7 web server (running on Windows Server 2008 R2 Standard Edition) and an SQL Server 2008 database server.
When debugging the website functionality that provokes the timeout, we notice that the connection itself takes milliseconds to complete, but the SqlCommand, which invokes a stored procedure on the database, hangs for several minutes during execution, then raises the timeout exception.
On the other hand, when we run the stored procedure directly on the database, it takes only 2 seconds to correctly finish execution.
We already tried the following:
Modified SqlCommand timeout on the website code
Modified execution timeout on the web.config file
Modified sessionState timeout on the web.config file
Modified authorization cookie timeout on the web.config file
Modified the connection timeout on the website properties on IIS
Modified the application pool shutdown time limit on IIS
Checked the application pool idle timeout on IIS
Checked the execution timeout on the SQL Server properties (it's set to 0, unlimited)
Tested the stored procedure directly on the database with other parameters
We appreciate any help.
Nirav
I've had this same issue with a stored procedure that was a search feature for the users. I tried everything, include ARTIHABORT etc. The SP joined many tables, as the users could search on anything. Many of the parameters for the SP were optional, meaning they had a default value of NULL in the SP. Nothing worked.
I "fixed" it by making sure my ADO.NET code only added parameters where the user selected a value. The SP went from many minutes to seconds in execution time. I'm assuming that SQL Server handled the execution plan better when only parameters with actual values were passed to the SP.
Note that this was for SQL Server 2000.
A few years ago I had a similar problem when migrating an app from SQL2000 to SQL2008.
I added OPTION (RECOMPILE) to the end of all the stored procs in the database that was having problems. In my case it had to do with parameters that were very different between calls to the stored proc. Forcing the proc to recompile will force SQL to come up with a new execution plan instead of trying to use a cached version that may be sub-optimal for the new params.
And in case you haven't done it already, check your indexes. Nothing can kill db performance like lack of a badly needed index. Here is a good link (http://sqlfool.com/2009/04/a-look-at-missing-indexes/) on a query that will display missing indexes.
Super-super late suggestion, but might come handy for others: A typical issue I saw and rather applicable to Java is the following:
You have a query which takes a string as a parameter. That string is search criterion on a varchar(N) column in the database. however, you submit the string param in the query as Unicode (nvarchar(N)). This will result in a full-table scan and conversion of every single field values to Unicode for proper comparison, to avoid potential data loss (if SQL Server converted the input param to non-Unicode, it may lose information).
Simple test: run the query twice (for the sake of simplicity, I'm assuming it's an SP):
exec spWhatever 'input'
exec spWhatever N'input'
See how they behave. Also, you may want to take a look at the Recent Expensive Queries section on the Activity Monitor in SSMS and ask for the execution plan, to clarify the situation.
Cheers,
Erik

DB timeout when generating report from the web

I've got a report in ASP.NET app. When I trying to generate it from the browser it crashes with DB timeout error, but when i'm executing exact same query in SQL Management Studio it shows the result set within 5 seconds.
Query is written unclean SQL in the code-behind file (no ORMs are used), it's parameters are from the web form, so i know what exact generated query will be.
What can be the cause of the problem?
First, use SQL Profiler to attach to the database and see exactly what query is being sent. Use that for other testing.
Second, set your connection timeout to something ridiculous like 300 seconds. Then do the same thing for the command timeout.
Third, make sure both your application and your management studio instance are talking to the same database... Preferably with the exact same user rights.
Run again. Then run it again.
It's possible that the database is taking time to do an initial load (hence the first query taking awhile) and the query through management studio is executing while the database is still "hot" so to speak.
Finally, you say that management studio shows results within 5 seconds.. Is it 5 seconds for it to start populating the query results window or 5 seconds for the entire query to finish executing. Those can be radically different times.

LINQ to SQL Stored Procedure. Exception says it times out but it is not timing out

Website using .NET Framework v3.5, SQL Server 2008, written in C#
I have a stored procedure which I have added to my DBML by dragging it across from the server explorer.
In it's properties it returns Auto-generated type.
The procedure takes < 1 second to run from within SQL Mgmt Studio for all inputs.
However from the code for 1 particular input (which takes < 1 second in the Mgmt studio) it hangs and then throws:
System.Data.SqlClient.SqlException: Timeout expired.
This didn't always happen for this one input! It used to also work fine when called from the code. The last time it didn't work I deleted and re-added the same stored procedure to the DBML. This "fixed" it, and that input ran fine and in the same time as all the others. However this is not an adequate fix! It has happened again and I can't keep deleting and re-adding as required.
I made no changes to the data that's being returned during the point at which it was "fixed", so I can't think what the problem could be. Any help on this would be much appreciated!
Exception says it times out but it is
not timing out
If it says it's timing out, it's timing out. The only question is "why"?
Run a SQL Server Profiler trace against your database and see what query is actually going to the server. It's possible that another query is being issued too. It's possible there is another transaction interfering in your production scenario.
It turns out that this is parameter sniffing - this is explained in another post: Executing stored proc from DotNet takes very long but in SSMS it is immediate
Also, be sure that the stored procedure is not being held up inside of a transaction, waiting for another process to complete. I just ran across this with a Linq to Sql stored procedure being called multiple times within a transaction. It gave me a timeout expired error and I just realized it was waiting for a previous call to complete, and thus timing out.

Resources