2013-05-03 2006 views
34

我正在运行一个ASP.NET Web页面页面,该页面在初始加载时从SQL服务器中拉出项目列表。这个查询在一秒钟左右运行,并在2秒内加载页面。回报大约有1000条记录,给予或承担。我从Service Manager SQL数据库中提取主机名以及一些其他信息。Win32Exception(0x80004005):等待操作超时

在这个页面中,我有一个内置的搜索,本质上运行完全相同的查询,但是使用基于主机名的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

回答

20

所有那些谁知道的比我多,而不是将其标记无益或误导,再读一遍。由于所有资源都被锁定的线程占用,我的虚拟机(VM)无法响应,因此,杀死线程是我唯一的选择。我不会向任何正在运行长时间查询的人推荐此功能,但可能会帮助那些无法响应VM的用户。它的最新个人接听电话。是的,它会杀死你的查询,但它保存了我的VM机器被破坏。

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

您需要运行以下命令来解决索引问题。

exec sp_updatestats 
+1

这适用于我! :) – BugLover 2014-06-10 15:59:22

+16

这不是一个普遍适用的答案,即查询的运行时间可能超过任何给定的超时值,而不管最近索引统计信息是否已更新。 – 2014-06-24 12:59:32

+1

确保在运行之前阅读并理解exec sp_updatestats的后果。 – redoc 2015-03-17 15:17:09

29

您遇到的问题是查询命令花费的时间太长。我相信查询执行的默认超时时间是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 
+0

我刚刚在一个Web应用程序中解决了这个问题,因为查询确实需要很长时间 - 因为没有人在被查询的字段上为数据库创建索引!这应该是第一个呼叫端口:) – 2017-03-08 11:38:47

3

我想在这里其他的答案以及其他几个人。我甚至停止并重新启动SQL服务。没有工作。

但是,重新启动我的电脑确实有效。

+0

Yupe,对于我也重新开始工作。 – incomplete 2015-11-15 16:29:22

+0

这工作一段时间,但问题出现,并需要另一次重新启动,是否有一个实际的永久修复? – BlackICE 2016-02-23 15:28:19

+0

@BlackICE我还没有回来。 – MiniRagnarok 2016-02-23 15:32:24

5

我有同样的问题。运行exec sp_updatestats有时会奏效,但并非总是如此。我决定在我的查询中使用NOLOCK声明来加速查询。 后您的FROM子句,e.g只需添加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

+0

当您的服务器执行大量多用户工作或大量长时间运行的报告时,NOLOCK是一个很好的解决方案! – 2016-02-02 12:07:09

+3

对于大多数问题,NOLOCK通常是一个不好的解决方案。 (http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/): 1)“脏读” 2)缺失行 3)读两次 4)读取多个版本的同一行 5)索引损坏 6)读取错误 等等。只是谷歌'NOLOCK坏主意'...(附注快照隔离可能是你的朋友在某些情况下) – HansLindgren 2016-03-08 09:02:08

+1

我不同意使用NOLOCK是“通常是对大多数问题的不好解决方案。”这实际上取决于手头的问题,并且你明白NOLOCK正在做什么。有些情况下,数据的整体完善并不十分重要。还有一些情况 - 在这种情况下,应尽量减少查询时间/锁定表所花费的时间。 – Paul 2016-06-27 13:20:33

2

我们2008年至2014年的SQL Server在那里我们我们的一些地方发展上一个连接字符串中有一个数据源,在升级之后遇到此错误= /像这样

 <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;"/> 
+0

从SQL Server 2012升级到2014年后,我遇到了同样的问题。SQL Server和Web服务器位于不同的机器上,但将数据源从“SERVER”更改为“SERVER.domain.local”_seems_以修复它。 – Darren 2016-03-11 10:39:52

0

我的表没有主键,然后我有超时错误。在设定的关键点之后。

4

如果您使用实体框架,你可以扩展默认的超时时间做(给长时间运行的查询更多的时间来完成):

myDbContext.Database.CommandTimeout = 300; 

哪里myDbContext是你的DbContext例如,而300是以秒为单位的超时值。

(语法当前为实体框架6)