Stored procedure returns "Timeout expired" - c#

In my Windows application, I use SQL Server 2008. My database size is 5086080 KB. Now I get the error as timeout expired when saving a transaction by using a stored procedure. So I set command timeout to 1200. It works fine. But I think it shouldn't because insert data have 2 or 3 lines. Is there any other way to solve this problem?
This is detail error message:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding .

Timeout is entirely on how long the actual SQL command is likely to take.
For example example, most of our commands run sproc's that should take no longer than 30 seconds to complete, however there are a couple that run for much longer, which means they have their own high valued timeouts.
You'll need to profile how long on average your routine takes then adjust the timeout accordingly, and remember to leave room for variables like latency etc

you need to profile your sql query and your code at every step. only then will you be able to know the exact bottleneck in your program.

Is somebody else keeping a transaction open that is holding up your query? Run sp_who or sp_who2 on the server to see what else is running.

Related

SQL Server TimeOut in C# After Adjusting timeout

I have a windows form application written in C# that passes a query to a SQL Server database and then displays the results in a dataviewgrid. The query that is passed to the database depends on the option selected in the form.
One particular query takes a little over a minute to run in management studio, but timeouts when it is passed to the database from the program. here are the steps I have done to try to resolve the situation:
Added a 5 minute timeout in the program by setting the connection timeout option to 300 seconds in the sql connection string. Example: Data Source=ab;Initial Catalog=abc;User ID=user; Password =pw; Connection Timeout=300
Setting the remote query timeout in the SQL Server instance to 0 (meaning, no timeout). Example: EXEC sp_configure 'remote query timeout', 0 ; GO
Neither of these options work. Despite implementing both of them, the c# program throws back a sql timeout error after less than a minute.
Is there a workaround for this? I have searched for this topic on stack overflow and so far all of the suggestions have been to do either 1 or 2 (which I have done).
For reference, I am using Visual Studio 17 Community edition and SQL Server 2016 Developer edition.
Any help would be greatly appreciated.
Thanks!
There's a "CommandTimeout" property on the SQL command. Try setting that.
Connection timeout and command timeout are two different things.
A connection timeout occurs when a connection cannot be retrieved from the connection pool within the allotted timeout period.
A command timeout occurs when a connection has been retrieved, but the query being executed against it doesn't return results within the allotted command timeout period. The default command timeout period in ADO.NET is 30 seconds.
If you've set the connection timeout to 300 seconds and you're still getting a timeout, it's likely a command timeout. As walkers01 said, set your command timeout to a suitable number of seconds. 300 seconds should be far more than sufficient; if your query executes in one minute in SSMS, a timeout of 90 seconds should suffice.
Try this for unlimited time of query execution if you are using SqlDataAdapter. I have use this one, solved my problem.
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
dscmd.SelectCommand.CommandTimeout = 0;

Sql connection for executing query

I have more than 12k records.
Default configured is 30 sec for connection time out. In this scenario i am getting exception like sql connection time out is closed.
I configured connection time out 0 for executing from c#.
Any disadvantage for set to value is 0.
Please suggest which value is fine for connection timeout. Please keep in mind I have huge data like 12k to 50k.
A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.
Making connection depends on database,drivers, it is local or remote.
You can not pin point some specific value knowing all these things. value doesn't depends on number of records.
if some problem occur while processing some specific record and it takes too long and you don't want to wait for so long in order to save time. Set CommandTimeout property but it has nothing to do with connection state

Sql connection timeout for a stored procedure execution from c#

I have 1 store procedure to generate some report, its is very complex so it is taking upto 7-8 mins sometimes to generate output.
When i am trying to access from the Webpage ( C# ) i am getting connection time out error.
I have already set remote connection timeout=0 (unlimited) and in connection string also i have tried to supply connection timeout.
I have suppose 6 Lacs around records of bills and i have performing 6 times around sum based on different groups and different dates, so is there any solution to make it faster.
Or any connection timeout workout?
I think the problem with page request timeout. I suppose you start stored procedure execution right on Page_Load event and after some time IIS close request by timeout.
I'm suggesting to you remove load function from Page_Load event and after page loaded send AJAX request to server or page to start stored procedure execution and check execution result from time to time. When result will be ready you can get it by AJAX and display to user.
Is it really connection timeout that you should be worrying about?
Since you have a long-running command, please ensure that you set CommandTimeout on your SqlCommand to 0.

What Happens To a Query If It Times Out?

Let's say I have a query that is sent to my SQL-Server database, it takes more than 30 seconds, and my program throws an SQL Query Timeout exception. Is the query still chugging along on my database or does it get terminated as soon as the exception is thrown?
A client signals a query timeout to
the server using an attention event.
An attention event is simply a
distinct type of TDS packet a SQL
Server client can send to it. In
addition to connect/disconnect, T-SQL
batch, and RPC events, a client can
signal an attention to the server. An
attention tells the server to cancel
the connection's currently executing
query (if there is one) as soon as
possible. An attention doesn't
rollback open transactions, and it
doesn't stop the currently executing
query on a dime -- the server aborts
whatever it was doing for the
connection at the next available
opportunity. Usually, this happens
pretty quickly, but not always.
Source There's no such thing as a query timeout...
When the client decides that the command has run long enough, it issues an "Abort". The query simply stops running in the database.
Any CATCH block won't be hit, transactions will be left open and locks can still remain allocated after this, even if the connection is closed because "close" means "return to connection pool".
If you expect a lot of Command Timeouts then consider using SET XACT_ABORT ON (and this too) that will release locks and rollback transactions. or fix the code...
Before executing a query, SQL Server
estimates how much memory it needs to
run and tries to reserve this amount
of memory from the buffer pool. If the
reservation succeeds the query is
executed immediately. If there is not
enough memory readily available from
the buffer pool, then the query is put
into a queue with a timeout value,
where the timeout value is guided by
the query cost. The basic rule is:
higher the estimated cost is, larger
the time out value is. When the
waiting time of this query exceeds the
timeout value, a time out error is
thrown and the query is removed from
the queue.
Source
If you get a SQL timeout then SQL has stopped, however web applications can time out and the SQL query can continue.
Usually when it times out it means the connection has died, meaning the query has not been sent to the database, most database support Transactions where you can start a transaction, run your queries, and if your happy you can commit them.
Example:
BEGIN TRAN
UPDATE authors
SET au_fname = 'John'
WHERE au_id = '172-32-1176'
UPDATE authors
SET au_fname = 'Marg'
WHERE au_id = '213-46-8915'
COMMIT TRAN

what may cause a c# application to stop after 12 hours

I am developing a c# utility that updates a datatable in a database. My application stops after 12 hours showing a messagebox indicating windows error.
I am sure that there is no memory leak. Moreover, it was also stopping around 12 hours of running indicating timeout expired message. So i set the Commandtimeout property to infinity. Now this error is no more showing.
what else could be stopping the utility after 12 hours? and what does this "Windows error" message mean?
I used AppDomain.CurrentDomain.UnhandledException to handle unhandled exceptions, and i got the following message box after running the utility for long time.
Messagebox: "Timeout Expired.The timeout period elapsed prior to completion of the operation or the server is not responding"
knowing that i am using SQLServer2008 R2 and i already set all the commandTimeout to 0.
whats wrong? why i am still getting this error ?!
Why does you want your utility to work continuesly, if some thing is to carry on at regular interval then you should use the schedular for invoking your utility. This will make sure that your application is not keeping the resource in locked state.
Now coming to your point of view... if application is must to run more than 12 hrs conti. then check,.. is there any thing that makes all the connection to break forcefully example backup scheduled for the database. this will make all the connection to break even if timeout is set to infinity.
Which database are you using? I know that in mysql there is a limit from the database about how much time the connection is kept open. Maybe in your database it is 12 hours,however if this is mySql it is a bit of a problem to change the timeout setting. So remember to change both the global and local setting or else it won't change, good luck.
Some networks have a hickup every 12 hours to crash p2p applications. Maybe there is some network related problem triggered every X hours.
Is you're code correctly going to reconnect on a failure and log decent error messages or is it just going to crash?

Categories

Resources