c# - 系统.Data.SqlClient.SqlException : Timeout expired

标签 c# asp.net sql-server-2008 iis-7

几天前,我注意到我的 Web 应用程序给我的 sql 异常超时已过期。

我清理了几个占用更多 CPU 的存储过程并重新启动了 SQL Server 服务,我的应用程序开始像以前一样快速工作,没有任何延迟。三四个小时后,我再次检查它,但我无法加载页面,因为它给了我异常超时过期。我检查了服务器 CPU 是否正常,一切看起来都很好。我在同一个 IIS 7 下还有一些其他网站,它们运行良好,没有任何异常。我再次重新启动 SQL Server 服务,我的应用程序再次恢复正常。 我认为这看起来像是 SQL Server 数据库上的问题,但我不知道如何解决它。

所以每次当我遇到异常时,我都会重新启动 sql 服务,但这当然不是最好的方法。请帮我解决这个问题。

这是我遇到的异常之一。

Message: Exception of type 'System.Web.HttpUnhandledException' was thrown. Source: System.Web Inner Exception:System.Data.UpdateException: An error occurred while updating the entries. See the InnerException for 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. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) --- End of inner exception stack trace --- at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave) at System.Data.Objects.ObjectContext.SaveChanges() at BCSCDomain.Domain.DataLayer.OtherDataLayer.UpdateHitCounter(Int32 hlistid, Int32 hcounterid) at BuyCarandSellCar.UsedCarProfilePage.HitCounter() at BuyCarandSellCar.UsedCarProfilePage.Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) Stack Trace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.usedcarlistings_profilepage_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\79794658\835d6695\App_Web_kmrmpdbb.16.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

最佳答案

首先,函数“UpdateHitCounter”最接近地导致了问题,但您不知道这是原因还是结果。您的查询超出了分配完成的配置时间。

每当您的数据库性能不佳时,您可以在其上运行的一种粗略方法是将 Sql Profiler 附加到它,然后根据结果执行索引调整向导。

如果您想采取更加谨慎的分类方法,您可以限制探查器仅记录需要超过 x 秒才能完成的查询,无论您认为 x 应该是什么。我通常从 5 点开始,如果没有任何结果,就从那里开始工作。 Here是该主题的入门读物。

一旦识别出长时间运行的查询,就在本地副本中执行它们并检查执行计划。 Here是入门,但首先要寻找“表扫描”。

最终,要么您的数据库未达到最佳状态,要么您的硬件无法满足流量需求。几乎可以肯定是第一种,这两种方法应该可以帮助您上路。

关于c# - 系统.Data.SqlClient.SqlException : Timeout expired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4719841/

相关文章:

c# - 实现 SOLR.Net 和 LUCENE.Net

c# - 在 ListView 中显示数据表中的图像

c# - 如何在从整数数组创建的列表中使用 ANY?

c# - EntityFramework EF 不让我重新搭建脚手架

c# - 如何使用 selenium 3.141.0 在 4.0.0 版上初始化 appium android 驱动程序

Asp.net 如何在 ListView 中添加链接按钮并进行一些编程

c# - 将 <ItemTemplate> 与复选框和标签一起使用

sql - 如何为 SQL 中的列组合提供唯一约束,而不考虑 Order

sql - 在 SQL Server 中选择 if 条件值

c# - 将 DateTime 插入 sql server 2008 时出现奇怪的错误