超时已过期。在操作完成之前,超时时间已经过去或服务器未响应。该语句已终止。

412
我网站上有很多用户(每天20000-60000人次),这是一个移动文件下载站点。我可以远程访问我的服务器(Windows Server 2008-R2)。我之前收到过“服务器不可用”错误,但现在看到连接超时错误。我对此不熟悉 - 为什么会发生这种情况,如何修复它?
完整的错误信息如下:
Server Error in '/' Application. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. [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) +404 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +412 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1363 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6387741 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +6389442 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +538 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +689 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +327 NovinMedia.Data.DbObject.RunProcedure(String storedProcName, IDataParameter[] parameters, Int32& rowsAffected) +209 DataLayer.OnlineUsers.Update_SessionEnd_And_Online(Object Session_End, Boolean Online) +440 NiceFileExplorer.Global.Application_Start(Object sender, EventArgs e) +163 [HttpException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +4052053 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +191 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +352 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375 [HttpException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11686928 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4863749
EDIT AFTER ANSWERS: 我在Global.asax中的Application_Start如下:
protected void Application_Start(object sender, EventArgs e)
{
    Application["OnlineUsers"] = 0;

    OnlineUsers.Update_SessionEnd_And_Online(
        DateTime.Now,
        false);

    AddTask("DoStuff", 10);
}

被调用的存储过程是:

ALTER Procedure [dbo].[sp_OnlineUsers_Update_SessionEnd_And_Online]
    @Session_End datetime,
    @Online bit
As
Begin
    Update OnlineUsers
    SET
        [Session_End] = @Session_End,
        [Online] = @Online

End

我有两种获取在线用户的方法:

  1. 使用 Application["OnlineUsers"] = 0;
  2. 另一种方法是使用数据库

所以,对于第二种方法,我在 Application_Start 中重置了所有在线用户。该表中有超过482,751条记录。


2
如此写道[默认为15秒] (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx) - V4Vendetta
1
最好进行根本原因分析,造成这种问题的原因有很多。最基本的是查询结构复杂。当我从表中提取存储为十六进制值的图像时,我遇到了相同的问题。 - Vijay Kumbhoje
除了上述原因,我还要补充一个:锁定超时:https://learn.microsoft.com/en-us/sql/t-sql/statements/set-lock-timeout-transact-sql 如果此线程等待锁定时间过长,则会根据上述文档超时。 - Herbert Yu
在服务中重新启动SQL Server解决了我的问题。 - xinthose
注意:此错误的另一个可能原因是,如果您在C#/VB.NET代码中使用事务,然后从事务内调用其他访问数据库的函数/子程序。解决方法是将db传递给嵌套的函数/子程序,以便它们被视为同一事务的一部分。(注意:理想情况下,我建议不要在应用程序代码中执行事务;而是在SQL代码中执行。) - Taraz
26个回答

1
尝试
EXEC SP_CONFIGURE 'remote query timeout', 1800
reconfigure
EXEC sp_configure

EXEC SP_CONFIGURE 'show advanced options', 1
reconfigure
EXEC sp_configure

EXEC SP_CONFIGURE 'remote query timeout', 1800
reconfigure
EXEC sp_configure

然后重新构建您的索引。

13
你能解释一下你的解决方案吗? - Hp93
如何重建索引? - Kiquenet

0

正如其他人所说,问题可能与未决事务有关。 在我的情况下,我必须将DbTransaction变量作为参数发送到ExecuteScalar方法中,以便正确执行该过程。

之前:

ExecuteScalar(command)

之后:

ExecuteScalar(command, transaction)

0
我在我的多线程程序中遇到了同样的错误,该程序有超过2000个用户同时连接。当我运行一个SELECT命令并返回超过5000行时,就会出现问题。这个命令被插入命令阻塞了。通过将SELECT *更改为SELECT Top(n) *,其中n < 5000,可以解决这个问题。

0

同时,您需要检查逻辑中是否未更新单个记录,因为在该位置使用更新触发器也会导致超时错误。

因此,解决方案是确保在循环/游标之后执行批量更新,而不是在循环中一次更新一个记录。


0
你是不是忘记了在异步语句前加上await关键字?如果你的任务花费的时间比线程中止的时间长,也会得到类似的消息提示。

0
超时,因为 SQL 查询花费的时间超过了你在 sqlCommand.CommandTimeout 属性中设置的时间。显然,你可以增加 CommandTimeout 来解决这个问题,但在这样做之前,你必须通过添加索引来优化查询。如果你在 Sql server management studio 中运行查询,并包括实际执行计划,则 Sql server management studio 将建议适当的索引。如果你能优化查询,大多数情况下都能摆脱超时问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接