【问题标题】:Windows Service SQL timeout exceptionWindows 服务 SQL 超时异常
【发布时间】:2012-10-12 23:43:36
【问题描述】:

我有一个 Windows 服务,它每 10 秒连续查询一次 SQL Server 数据库表,我还有一个网站,它也在 25 秒后查询同一个数据库表。

一段时间后我得到异常 - server timeout exception has occurred。我浏览了 stack-overflow 站点并对 SQL 配置设置进行了更改,但仍然遇到相同的异常。怎么办?

堆栈跟踪

在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection) 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔型 breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler、SqlDataReader 数据流、 BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlDataReader.ConsumeMetaData() 在 System.Data.SqlClient.SqlDataReader.get_MetaData() 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,字符串 resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior runBehavior、布尔 returnStream、布尔 异步)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior、runBehavior、布尔返回流、字符串 方法,DbAsyncResult 结果)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior、runBehavior、布尔返回流、字符串 方法)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)在 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 行为)在 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior 行为)
在 Microsoft.Practices.EnterpriseLibrary.Data.Database.DoExecuteReader(DbCommand 命令,CommandBehavior cmdBehavior)在 Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteReader(DbCommand 命令)在 Microsoft.Practices.EnterpriseLibrary.Data.CommandAccessor1.<Execute>d__0.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 源) 在 System.Runtime.Remoting.Messaging.Message.Dispatch(对象目标,布尔 fExecuteInContext) 在 System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)

【问题讨论】:

  • 首先向我们展示完整的堆栈跟踪和异常消息。
  • 您需要确定发生这种情况的位置。是连接,是事务,命令等。使用一些日志记录来确定错误在哪里并发布异常跟踪。这应该会给你一些关于什么需要调整的线索。就目前而言,您的问题就像读心术。

标签: asp.net .net sql-server-2008 windows-services


【解决方案1】:

给我看代码!超时异常通常是因为你没有关闭数据库连接。

尝试在算法的最后添加代码

if (connection.State == System.Data.ConnectionState.Open)
    {
        connection.Close();
        connection.Dispose();
    }

如果您使用 EnterpriseLibrary,请尝试为 commandTimeOut 添加更多时间

DbCommand dbcommand = database.GetStoredProcCommand("usp_TheStoredProcedureName");
dbcommand.CommandTimeout = 120;

【讨论】:

  • 我正在使用企业库并使用 DatabaseFactory.CreateDatabase() 方法,我正在获取数据库并针对它执行存储过程。所以需要关闭连接?
  • 超时已过期。在操作完成之前超时时间已过或服务器没有响应。声明已终止。
  • @RaviKhapate:请不要将代码示例或示例数据放入 cmets - 因为您无法格式化,所以非常难以阅读。 ... 相反:更新您的问题,通过编辑它来提供附加信息!谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
相关资源
最近更新 更多