Change the default SqlCommand CommandTimeout with configuration rather than recompile?

不想你离开。 提交于 2019-12-12 10:46:59

问题


I am supporting an ASP.Net 3.5 web application and users are experiencing a timeout error after 30 seconds when trying to run a report. Looking around the web it seems it's easy enough to change the timeout in the code, unfortunately I'm not able to access the code and recompile. Is there anyway to configure the default for either the web app, the worker process, IIS or the whole machine?

Here is the stack trace up to the point where it's in System.Data in case I'm missing some other problem:

[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) +1948826
  System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4844747
  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(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +162
  System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +115

--Edit

There must be something outside the code itself - I've downloaded the database and run it against the same web site installed on a test server and it runs for longer than 30 seconds and returns the report. I've compared the machine.config and web.config files from the .Net directory on the live and test and they seem the same, compared the two IIS setups, also looked at the SQL Server configuration and the only difference is that the live server is clustered on 64bit W2K3 while the test server is on 32bit.


回答1:


Unfortunately, everything I've read about this in the past says no, can't be controlled via configuration. Which is a bummer.




回答2:


You cannot set the command timeout from the connection string or some external setting, sorry.

Sometimes a driver will interpret the 'timeout' parameter in the connection string as both Connect Timeout and Command Timeout period, but that is technically a "bug". Gotta love the standards.




回答3:


The best I know of is you can set a Connection Timeout in the connection string which controls how long ADO will wait while trying to connect to SQL Server and doesn't impact the Command Timeout.

Personally I think there should be a way to change this setting. Assuming the assembly this is in is not strong named your best bet might be to reverse engineer the code modify the source and redeploy the assembly with the new name.




回答4:


It's not too hard to roll your own though? Just create a custom SqlCommandTimeout setting under AppSettings. If you are already using some sort of SqlCommandFactory to create your SqlCommand or have some sort of SqlCommand base class then you can easily make the change. If not then you probably don't care enough about your code to want to put this in a configuration file anyway.

Regards, James



来源:https://stackoverflow.com/questions/1001713/change-the-default-sqlcommand-commandtimeout-with-configuration-rather-than-reco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!