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

Bol*_*n32 48 sql-server-2008 asp.net-webpages

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

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

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

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


The wait operation timed out 
Run Code Online (Sandbox Code Playgroud)

描述:执行当前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
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

[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
Run Code Online (Sandbox Code Playgroud)

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

Jef*_*eff 32

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

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

e.Command.CommandTimeout = 60
Run Code Online (Sandbox Code Playgroud)

例如:

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

  • 我刚刚在一个 Web 应用程序上解决了这个问题,其中查询确实花费了太长时间 - 因为没有人在数据库上为正在查询的字段创建索引!这应该是第一个停靠港:) (2认同)

San*_*lke 23

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

Serverstack已经回答了类似的问题.它解决了我在VM机器上使用SQL的问题.请点击这里

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

exec sp_updatestats
Run Code Online (Sandbox Code Playgroud)

  • 这不是一般适用的答案,即无论最近是否更新了索引统计信息,查询都可以比任何给定的超时运行更长时间. (16认同)

Jon*_*der 9

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

myDbContext.Database.CommandTimeout = 300;
Run Code Online (Sandbox Code Playgroud)

myDbContext您的DbContext实例在哪里,并且300是以秒为单位的超时值.

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


Min*_*rok 5

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

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


Flo*_*Flo 5

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

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

阅读完整的文章在这里.

  • 对于大多数问题,NOLOCK通常是一个糟糕的解决方案.(http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/):1)"脏读"2)缺少行3)读取行两次4)读取同一行的多个版本5)索引腐败6)阅读错误等.只是谷歌'NOLOCK坏主意'...(在某些情况下,PS Snapshot Isolation可能是你的朋友) (4认同)
  • 我不同意使用NOLOCK"通常是解决大多数问题的坏方法".这真的取决于手头的问题,你了解NOLOCK正在做什么.在某些情况下,数据的整体完善并不是非常重要.还有一些情况 - 在这种情况下,您应该最小化使用锁定表所花费的查询时间/时间. (3认同)

nii*_*ico 5

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

您可以首先找出碎片级别 - 如果它高于 10% 左右,您可以从重新索引中受益。如果它非常高,则可能会造成严重的性能瓶颈。

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

这应该定期进行。