How can you increase timeout in Linq2Entities? - c#

I'm doing a basic select against a view. Unfortunately the result can be slow and I'm getting timeout errors intermittently. How can I increase the timeout?
Using .NET 3.5, Sql Server 2000, Linq2Entities
I'm using the very basic query List<MyData> result = db.MyData.Where(x.Attribute == search).ToList();
Fixing the query so that it's faster on the DB side is not an option here.
Exact Error: "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."
Update: I'd prefer to just change it for this one query.

You can set the timeout in your connection string.
Edit (new):
It turns out that there are two different timeout concepts. The connection timeout is used to determine wether or not a connection can be established. The CommandTimeout property on the object context controls timeout for commands. So just set that to a high value, and it should not influence the short-running queries in any way.

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;

Stored procedure returns "Timeout expired"

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.

Exception in use ExecuteScalar

I want to get a person from database in C# with ADO.net.
I use this command:
System.Data.Common.DbCommand command = DatabaseManager.Database.GetStoredProcCommand(proc_name, firstName);
and then
object personID = DatabaseManager.Database.ExecuteScalar(command);
When I click a button, these commands calls two times.
In the first time it is OK. But the second time, I get TimeOutException:
Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding.
I used command.Disposed(); and command.Connection.Close();. But these not resolve my problem.
Please help me how I can resolve this problem.
It seems that some transaction is left open during your first execution of the procedure. This can be either by an error in your procedure or bad connection management in your application.

Nhibernate Connection Pool Problems

we are having some connection pool issues with Nhibernate on an MVC3 web application which is running with SQL Express and dealing with multiple concurrent AJAX based requests.
Every so often (hours in between) we see errors starting which show:
NHibernate.Util.ADOExceptionReporter
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
then a load of
While preparing select TOP (#p0)
....
an error occurred
We have to recycle the IIS app pool to stop 500 errors from being thrown after that.
Looking at the SQL Server we see:
select * from sys.dm_exec_sessions
... gives about 30 sessions with IDs above 51 (i.e. user sessions)
select * from sys.dm_exec_connections
... gives around the same amount
BUT
select ##connections
... gives results with 79022
Is this indicating that the connections are never released?
The Nhibernate sessions are for the lifetime of the request.
Does anyone have any experience of anything like this or can point us in the right direction?
Many thanks
Richard
You can't have more then 32767 connection to SQL Server.
##CONNECTIONS also gives (my bold)
Returns the number of attempted connections, either successful or unsuccessful since SQL Server was last started.
Not current connection
I suspect that your pool is not set up correctly so it's exhausted too quickly.
Or you are not releasing connections correctly and you're checking SQL Server after you recycle IIS.

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

Categories

Resources