none
An error occurred while executing the command definition, w/ inner exception SqlException

    Question

  • Hi all,

    In a MVC 3 project we are using EF. Currently I encounter an exception - appearently at random - and can't find a solution. The inner exception says something about a timeout, but the exception occurs immediatly after executing the code. When I catch the exception and re-execute the code, then the command is executed correctly. This occurs while inserting, updating and reading (currently we don't delete anything). The EntityContext is attached to the HttpContext, and thus refreshed with every call. Below the exception:

    27-5-2011 14:48:07: HandlingInstanceID: 9dc30073-2a4d-4f99-a9a2-cc261bb443d8
    
    An exception of type 'System.Data.EntityCommandExecutionException' occurred and was caught.
    
    -------------------------------------------------------------------------------------------
    
    05/27/2011 14:48:07
    
    Type : System.Data.EntityCommandExecutionException, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    
    Message : An error occurred while executing the command definition. See the inner exception for details.
    
    Source : System.Data.Entity
    
    Help link : 
    
    Data : System.Collections.ListDictionaryInternal
    
    TargetSite : System.Data.Common.DbDataReader ExecuteStoreCommands(System.Data.EntityClient.EntityCommand, System.Data.CommandBehavior)
    
    Stack Trace : at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
    
     at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
    
     at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
    
     at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
    
     at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
    
     at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
    
     at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
    
     at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
    
     at System.Linq.Queryable.Single[TSource](IQueryable`1 source)
    
     at MyCustomer.MyProject.Repositories.MyEntityRepository.RetrieveSingleWithLookup(Int32 id) in D:\...\MyCustomer.ITS\MyProject\Main\MyProject\03. Data Access\MyCustomer.MyProject.Repositories\MyEntityRepository.cs:line 158
    
    
    
    Additional Info:
    
    
    
    MachineName : ...
    
    TimeStamp : 27-5-2011 12:48:07
    
    FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
    
    AppDomainName : 186a108f-7-129509718575394192
    
    ThreadIdentity : ...
    
    WindowsIdentity : ...
    
    	Inner Exception
    
    	---------------
    
    	Type : System.Data.SqlClient.SqlException, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    
    	Message : Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
    
    	Source : .Net SqlClient Data Provider
    
    	Help link : 
    
    	Errors : System.Data.SqlClient.SqlErrorCollection
    
    	Class : 11
    
    	LineNumber : 0
    
    	Number : -2
    
    	Procedure : 
    
    	Server : localhost\sql2008r2
    
    	State : 0
    
    	ErrorCode : -2146232060
    
    	Data : System.Collections.ListDictionaryInternal
    
    	TargetSite : Void OnError(System.Data.SqlClient.SqlException, Boolean)
    
    	Stack Trace : at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
    
    	 at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
    
    	 at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
    
    	 at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
    
    	 at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
    
    	 at System.Data.SqlClient.SqlDataReader.get_MetaData()
    
    	 at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
    
    	 at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
    
    	 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
    
    	 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
    
    	 at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
    
    	 at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
    
    	 at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
    
    	 at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
    
    
    
    

    I am runing Visual Studio 2010 SP1, .NET 4.0, MVC 3, SQL Server 2008 R2.

    Who can help me solve this issue?

    Kind regards,

    Ronald


    Friday, May 27, 2011 1:59 PM

All replies

  • Hi Ronald,

    Welcome!

    Thanks for your error exceptionn, would you please feel free to give the error code? Thanks for understanding, by the way I think your problem may relate: http://msdn.microsoft.com/en-us/library/ee358769.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 30, 2011 7:52 AM
    Moderator
  • Hi Alan,

    Thanks for your answer. I can't see how the error relates to directly executing commands against the data source, as described in the link you referred to since I do not execute commands directly. Currently, I do no exclude the option, that the database simply is not responding (as the exception suggests), since I have not been able to reproduce the issue on SQL Azure. However, other projects are runnig on the database as well and do not encounter this issue. This issues also occurs at other databases (inlcuding my local). Please note that the error occurs on all operations (add, update, retrieve).

    In my code I use the same 'pattern' for all data access. As a sample:

    // Controller in MVC
    ...
     Incident incident = GetIncident(id);
    ...
    
     private Incident GetIncident(int incidentId)
    {
     return IncidentLogic.RetrieveFull(incidentId);
     }
    
    // Business logic assembly
     public static Incident RetrieveFull(int id)
    {
     return IncidentRepository.RetrieveSingleWithLookup(id);
     }
    
    // Repository
    public static Incident RetrieveSingleWithLookup(int id)
     {
     Incident incident = null;
      incident = MyContext.Incidenten.Where(i => i.Id == id).Single();
     return incident;
     }
    
    // MyContext overloads from a base repository. The context is stored on the HttpContext
     protected static IncidentRegistratieEntitiesContext MyContext
     {
     get
     {
     // We're using a context per HTTP request.
     var objectContext = GetHttpObjectContext();
    
     if (objectContext != null)
     {
      return objectContext;
     }
     // If all else fails:
     return _staticContext ?? (_staticContext = new IncidentRegistratieEntitiesContextFactory().GetNewContext());
     }
     }
    
    private static IncidentRegistratieEntitiesContext GetHttpObjectContext()
     {
     if (HttpContext.Current == null)
     {
     return null;
     }
     string objectContextKey = "ojbectContext_" + HttpContext.Current.GetHashCode().ToString("x", CultureInfo.InvariantCulture);
    
     if (!HttpContext.Current.Items.Contains(objectContextKey))
     {
     HttpContext.Current.Items.Add(objectContextKey, new IncidentRegistratieEntitiesContextFactory().GetNewContext());
     }
     return HttpContext.Current.Items[objectContextKey] as IncidentRegistratieEntitiesContext;
     }
    
    // Saving is about the same:
    
    // Controller:
     private void SaveIncident(Incident incident)
     {
     IncidentLogic.Save(incident);
     }
    
    // Busines logic:
     public static void Save(Incident incident)
     {
     if (incident.Id == 0)
     {
     incident.StatusIncidentId = (int)IncidentStatus.Nieuw;
     }
    
     IncidentRepository.Save(incident);
     IncidentRegistratieEntitiesRepositoryBase.SaveContext();
     }
    
    // Repository:
     public static void Save(Incident incident)
     {
     if (incident.Id > 0)
     {
     ;
     }
     else
     {
     MyContext.Incidenten.AddObject(incident);
     }
     }
    
     public static void SaveContext()
     {
     MyContext.SaveChanges(); 
     }
    

     Some additional information, I am running Windows 64bits and a SQL server 2008 64 bits.

    Kind regards,

    Ronald




    Monday, May 30, 2011 8:25 AM
  • Hi RonadId,

    Thanks for your feedback.

    You mean your problem occurs on SQL Azure, I'm not familiar with that. Every operator will cause the same error? I will do some pending research and come back as soon as possible, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 30, 2011 9:04 AM
    Moderator
  • Hi Alan,

    The problem does not occur on SQL Azure. That is, I haven't seen is yet on SQL Azure. It occurs at our local SQL Server 2008 databases.

    To further clearify myself, not every database action throws that error. Servel times all database actions succeed. Then, most database actions throw that particular error and succeed when retrying (retyr logic not shown in the code above).

    Hope you can find something. In the meantime: thanks for your time!

    Kind regards,

    Ronald

     

    Monday, May 30, 2011 10:03 AM
  • Hi all,

    I am getting the similar kind of error. please check the error code below. i breaking my head long one month. but clueless. this was happend in production only. sometimes its work fine and sometimes not. please give me solution

    System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)

       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()

       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

       at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()

       at System.Data.SqlClient.SqlDataReader.get_MetaData()

       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)

       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)

       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)

       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)

       at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)

       at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

       at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)

       at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

       --- End of inner exception stack trace ---

       at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

       at System.Data.Objects.ObjectContext.CreateFunctionObjectResult[TElement](EntityCommand entityCommand, EntitySet entitySet, EdmType edmType, MergeOption mergeOption)

       at System.Data.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, MergeOption mergeOption, ObjectParameter[] parameters)

       at OwensCorning.Carrier.Service.DataAccessLayer.OC_Carrier_Portal_TestEntities.spAccountActivitySummaryCount(String emailAddress)

       at OwensCorning.Carrier.Service.DataAccessLayer.HomepageDataAccessLayer.GetAccountActivitySummaryCount(String emailAddress)

       at OwensCorning.Carrier.Service.CarrierService.GetAccountActivitySummaryCount(String emailAddress)

       at SyncInvokeGetAccountActivitySummaryCount(Object , Object[] , Object[] )

       at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)

       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)

       at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)

    Wednesday, October 10, 2012 2:04 PM