如何增加存储过程调用的超时时间(请参阅错误)?

时间:2009-05-08 19:53:03

标签: asp.net sql-server

 Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
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.

Source Error:

Line 91: 
Line 92:             DataSet getData;
Line 93:             getData = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "Course_NewReportGet_Get_Sav", objPara);
Line 94: 
Line 95:             foreach (DataRow dr in getData.Tables[0].Rows)


Source File: c:\Users\Ryan\bancroft archive\santiago\santiago code\trunk\admin\tools\Optimus.aspx.cs    Line: 93

Stack Trace:

[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) +1950890
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
   System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
   System.Data.SqlClient.SqlDataReader.get_MetaData() +83
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +130
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +287
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +94
   Mexico.Data.SqlHelper.ExecuteDataset(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters) +149
   Mexico.Data.SqlHelper.ExecuteDataset(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) +93
   admin_tools_Optimus.GetUsers() in c:\Users\Ryan\bancroft archive\santiago\santiago code\trunk\admin\tools\Optimus.aspx.cs:93
   admin_tools_Optimus.GetCompanies() in c:\Users\Ryan\bancroft archive\santiago\santiago code\trunk\admin\tools\Optimus.aspx.cs:75
   admin_tools_Optimus.Proceed(Object sender, EventArgs e) in c:\Users\Ryan\bancroft archive\santiago\santiago code\trunk\admin\tools\Optimus.aspx.cs:43
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

6 个答案:

答案 0 :(得分:18)

您可以通过设置SqlCommand.CommandTimeout属性来提高命令的超时时间。

答案 1 :(得分:11)

也许这不适用,但我认为应该提到它。默认的CommandTimeOut是30秒。对于99.9%的Web应用程序中发生的任何操作,这应该是充足的时间。除非您完全确定该操作需要的时间超过该时间,否则您应该花时间尝试优化sql,使其返回<30秒。正确的索引是一个很好的起点。

答案 2 :(得分:10)

然后将其添加到连接字符串中:

server={servername};database={dbname};uid={username};pwd={password};Connect Timeout=600

答案 3 :(得分:2)

+1到Al,因为30秒应该足够了。延长超时时间实际上只是潜在问题的绷带。

根据我的经验,存储过程在90%的时间内过于昂贵。当我最后审查这些错误时,我有一个一般的经验法则,即没有存储过程的成本超过1.00。它们越贵,我们阻止和产生这些例外的风险就越大。

答案 4 :(得分:2)

使用的查询未针对数据量进行优化。在我们的例子中,我们清除了未使用的数据,但更好的选择应该是优化查询或查看内务处理。行动结束后,我们不再收到错误。当然,这取决于您收到此错误的情况以及此解决方案是否会帮助您。

答案 5 :(得分:1)

您可以通过在连接字符串中添加“Connect Timeout = 90”来完成此操作。如果您的查询花费的时间超过30秒,但您应该考虑以某种方式优化查询。可能需要添加索引或者可能需要创建视图等。

编辑以纠正@Stijn评论指出的问题。