Win32Exception(0x80004005):等待操作超时

问题描述 投票:38回答:9

我正在运行一个ASP.NET网页页面,在初始加载时从SQL服务器中提取项目列表。此查询在大约一秒钟内运行,并在2秒内加载页面。返回约1000条记录,给予或接受。我从Service Manager SQL数据库中提取Hostnames以及其他一些信息。

在这个页面中,我有一个内置的搜索,它基本上运行完全相同的查询,但使用基于主机名的LIKE运行它。这将加载包含搜索查询一部分的所有主机名的同一页面。查询通常在一秒钟内在SQL Management Studio中运行,但加载页面需要更长时间,有时会超时。

我的问题是,为什么基于参数的搜索需要更长的时间,有时会因为没有明显原因而超时。是否有任何步骤可以缓解此超时?以下是完整错误。

'/'应用程序中的服务器错误。


The wait operation timed out 

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

例外细节:

System.ComponentModel.Win32Exception: The wait operation timed out
Source Error: 

Line 13:     }
Line 14:     
Line 15:     var selectedData = db.Query(selectCommand, searchTerm);
Line 16: 
Line 17:

Source File:  c:\Users\u0149920\Documents\My Web Sites\AppSupport\servers\default.cshtml    Line:  15

堆栈跟踪:

[Win32Exception (0x80004005): The wait operation timed out]
[SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1753346
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295154
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +59
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +1325
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +175
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
   System.Data.Common.DbCommand.ExecuteReader() +12
   WebMatrix.Data.<QueryInternal>d__0.MoveNext() +152
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   WebMatrix.Data.Database.Query(String commandText, Object[] parameters) +103
   ASP._Page_servers_default_cshtml.Execute() in c:\Users\u0149920\Documents\My Web Sites\AppSupport\servers\default.cshtml:15
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +151
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +114

版本信息:Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

sql-server-2008 asp.net-webpages
9个回答
32
投票

您遇到的问题是查询命令花费的时间太长。我相信查询执行的默认超时是15秒。您需要设置CommandTimeout(以秒为单位),以便命令完成其执行的时间足够长。 “CommandTimeout”与连接字符串中的“Connection Timeout”不同,必须为每个命令设置。

在您的SQL选择事件中,使用命令:

e.Command.CommandTimeout = 60

例如:

Protected Sub SqlDataSource1_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
    e.Command.CommandTimeout = 60
End Sub

23
投票

对于所有比我更了解的人,而不是将其标记为无益或误导,请再次阅读。由于锁定线程消耗了所有资源,因此我的虚拟机(VM)无法响应,因此杀死线程是我唯一的选择。我不建议那些运行长查询的人,但可能会帮助那些陷入无法响应VM的人。它可以接听电话。是的,它会终止您的查询,但它保存我的VM机器被销毁。

Serverstack已经回答了类似的问题。它解决了我在VM机器上使用SQL的问题。请检查here

您需要运行以下命令来修复索引问题。

exec sp_updatestats

6
投票

如果您正在使用实体框架,则可以通过执行以下操作来扩展默认超时(以便为长时间运行的查询提供更多时间来完成):

myDbContext.Database.CommandTimeout = 300;

其中myDbContext是您的DbContext实例,300是以秒为单位的超时值。

(实体框架6中的语法current)


5
投票

我遇到过同样的问题。运行exec sp_updatestats有时会工作,但并非总是如此。我决定在查询中使用NOLOCK语句来加速查询。只需在FROM子句后添加NOLOCK,例如:

SELECT clicks.entryURL, clicks.entryTime, sessions.userID
FROM sessions, clicks WITH (NOLOCK)
WHERE sessions.sessionID = clicks.sessionID AND clicks.entryTime > DATEADD(day, -1, GETDATE())

阅读完整的文章here


4
投票

我在这里尝试了其他答案以及其他一些答案。我甚至停止并重新启动了SQL服务。没有任何效果。

但是,重新启动计算机确实有效。


4
投票

查看数据库中的重新索引表。

您可以首先找出碎片级别 - 如果它超过10%左右,您可以从重新索引中受益。如果它非常高,那很可能会产生一个重要的性能瓶颈。

http://blog.sqlauthority.com/2009/01/30/sql-server-2008-2005-rebuild-every-index-of-all-tables-of-database-rebuild-index-with-fillfactor/

这应该定期进行。


2
投票

从2008到2014 SQL Server升级后我们遇到了这个错误,我们之前的一些本地开发连接字符串有一个Data Source =。/ like

        <add name="MyLocalDatabase" connectionString="Data Source=./;Initial Catalog=SomeCatalog;Integrated Security=SSPI;Application Name=MyApplication;"/>

将./更改为(本地)或localhost可以解决问题。

<add name="MyLocalDatabase" connectionString="Data Source=(local);Initial Catalog=SomeCatalog;Integrated Security=SSPI;Application Name=MyApplication;"/>

1
投票

我的表没有主键,然后我有时间错误。在设置键解锁后。


0
投票

由于sqlcommand执行时间将发生此问题。设置CommandTimeout = 100或期望值

以秒为单位的超时值@Note更好地提供最佳值

© www.soinside.com 2019 - 2024. All rights reserved.