16

We have an MVC web application that I inherited that loads, let's call it MyCategory and all of its children into a webform, takes edit, then saves on save clicked. The way the application works is to first delete all data for the parent level entity in the model, then add everything back. We have pretty low concurrent user base on this, no more than twenty people saving at most every dew minutes each. We are unable to reproduce this error locally, but when the offshore team starts work we have started seeing the

[SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.]

error show up. This is happening when calling a delete from LINQ on the parent table (it has no data at this point, nor do child tables). It is intermittent, but has happened quite a few times in the past week, in which there was a ramp up in work on this portion of the project.

From the Stack Trace, it looks to be failing on System.Data.SqlClient.SqlCommand.FinishExecuteReader which appears to be going for 109+ minutes. This should be deleting at most tqo records from the table, and anyone loading data from this table should be retrieving at most two in a very short time span.

Any ideas on where to start would be appreciated. Unfortunately, I do not have permissions to run SQL Query Analyzer or Activity Monitor on the production database.

Call Stack is:

[SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
The statement has been terminated.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +717
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4515
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6557561
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +6560327
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +586
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +742
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +287
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +789
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +188
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500
   System.Data.Linq.StandardChangeDirector.DynamicDelete(TrackedObject item) +71
   System.Data.Linq.StandardChangeDirector.Delete(TrackedObject item) +258
   System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +622
   System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +932
   WebAppData.MyCategory.MyCategoryData.DeleteAll(Int32 id, Guid gid) +1053
   WebAppServices.MyCategory.MyCategoryService.DeleteMyCategoryParentItems(Int32 id, Guid gId) +1632
   WebAppServices.MyCategory.MyCategoryService.UpdateMyCategory(Int32 id, Guid gId, MyCategoryEntity mce) +51
   WebAppUI.Areas.Documents.Categories.Sections.MyCategory.MyCategoryController.Save(Int32 Id, Guid gId, MyCategoryModel model) +93

EDIT:

Connection String:

<add name="Data" connectionString="Data Source=myserver;Initial Catalog=mydatabase;User ID=myuser;Password=mypassword />
4
  • actually, this does sound like a server issue. Are you sure the server is available and responsive? Sep 8, 2016 at 18:56
  • can you show the connection string?
    – Sampath
    Sep 8, 2016 at 20:09
  • It could be a deadlock. It could also be the volume of deletes as the deletes from EF are executed line by line as opposed to a single delete statement that points to a range of records. If recommend you find someone that does have that permission so you can see what is going on.
    – Igor
    Sep 8, 2016 at 20:13
  • The connection string would help because there might be timeout or other parameters tolerating a deadlock.
    – n8wrl
    Sep 8, 2016 at 20:20

5 Answers 5

13

This is one temporary solution for your issue right now.You can increase the Timeout as shown below.But you must find out the EF query which it causes this issue and need to improve the performance it.

Note : here time is seconds

public DbContext() : base("Default")
{
    this.Database.CommandTimeout = 60;
}
0
11

Runs UPDATE STATISTICS against all user-defined and internal tables in the current database.

USE database_name;  
GO  
EXEC sp_updatestats;

Reference: https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-updatestats-transact-sql?view=sql-server-2017

2
  • 1
    Silly me for thinking MS SQL would keep its indexes up to date! Jul 3, 2020 at 23:18
  • Well done , very nice Apr 11, 2022 at 13:02
0

Make sure you didn't forgot to commit/rollback transaction.

Transaction makes your table lock from selecting/updating/deleting. And because of it, your accessing to delete will be time out just because for waiting when the table being unlock again.

-1

The short and to the point answer is when you are using Entity Framework then you must have to increase the timeout limit.

There are two ways to increase that limit

  1. For every request taking more time
  2. For only Single request

A. you can define the timeout limit on Db level as @Sampath mentioned,

context.Database.Timeout= 180;

B. you can define with the ADO.NET also, like this

sqlCmd.CommandTimeout = 60 * 30;

The major issue with your query that takes more time to load. Beyond this there is another concept SSIS(integration service) we can utilize that also.

-2

For a specific query, you can write the below code

context.Database.CommandTimeout =60*3;

context is the database connection object name 60*3 means 180 seconds, because CommandTimeout accepts value in seconds.

3
  • That repeats this answer:stackoverflow.com/a/39399494/861716 Oct 6, 2020 at 10:21
  • 1
    @GertArnold I don't think so. There are chances that someone need to put time out in a specific query. :) Oct 6, 2020 at 11:14
  • It's enough to know that CommandTimeout can be set. It's not necessary to spell out all possible use cases. Oct 6, 2020 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.