c# - 为什么数据库会超时?

标签 c# sql sql-server

当我对数据库运行查询时,我不断得到这个

System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Generated: Tue, 18 Aug 2009 08:05:39 GMT

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
   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.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Linq.Provider.BindingList.Create[T](DataContext context, IEnumerable`1 sequence)
   at System.Data.Linq.DataQuery`1.GetNewBindingList()
   at System.Data.Linq.DataQuery`1.System.ComponentModel.IListSource.GetList()
   at System.Windows.Forms.CurrencyManager.SetDataSource(Object dataSource)
   at System.Windows.Forms.BindingContext.EnsureListManager(Object dataSource, String dataMember)
   at System.Windows.Forms.BindingContext.get_Item(Object dataSource)
   at PlexityHide.GTP.TimeItemDataConnect.Wire()
   at PlexityHide.GTP.TimeItemDataConnect.UpdateDataSource(IListSource aDataSource, String aDataMember, Layer aLayer)
   at PlexityHide.GTP.TimeItemDataConnect.set_DataSource(Object value)
   at PlexityHide.GTP.Layer.set_DataSource(Object value)
   at WebSchedule._Default.Grid_OnEachListItemWhenDataBound_GridNode(Object GTPComponent, EachListItemWhenDataBoundArgs e) in C:\MyDev\WebSchedule\Schedule.aspx.cs:line 503
   at PlexityHide.GTP.Grid.NodeChangedInDS(GridNode aNode)
   at PlexityHide.GTP.NodeDataConnect.UpdateRowContent(Int32 index)
   at PlexityHide.GTP.NodeDataConnect.EnsureBindUpToDate()
   at PlexityHide.GTP.GridNodes.get_Count()
   at PlexityHide.GTP.GridStructure.get_FirstVisibleNode()
   at PlexityHide.GTP.Gantt.set_TurnOffAllCollisionDetect(Boolean value)
   at PlexityHide.GTP.WEB.Gantt_ASP.OnPreRender(EventArgs e)
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.BasePartialCachingControl.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception 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.schedule_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\13c4fe72\f906a8a8\App_Web_schedule.aspx.cdcab7d2.nhap4sin.0.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

会不会是任何时候连接到数据库的连接数引起的? 有问题的页面调用甘特图,并根据特定契约(Contract)使用时间表表中的数据填充它。正在发生的事情是,如果说有 10 个人同时调用 table ,我就会超时

最佳答案

您可能有太多长时间运行的查询。另一个很可能的原因是您没有在完成连接后立即关闭连接,这会将它们释放到连接池中。

通常,您应该使用using 语句尽可能晚地获取连接并尽早释放它们,以避免这种情况。如果您没有显式关闭连接(即使是在遇到异常时),您就会受到垃圾收集器和终结器的摆布,将连接返回到池中。

如果您相信您确实有许多与长时间运行的查询的事件连接,您应该增加连接池中的连接数。如果您认为是实际查询本身超时(即花费的时间比您允许的时间长),那么您应该增加命令或连接的超时时间。

关于c# - 为什么数据库会超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1294130/

相关文章:

C# .NET 对象处置

c# - WPF Telerik UI BusyIndi​​cator 不显示(UI 线程)使用 MVVM

mysql - 如何对不同行记录求和

sql - 更改默认列值会导致 Hibernate 出现问题吗?

sql - 获取缺少答案的调查问卷列表

c# - 使用 linq/Entity Framework 查询多对多关系。代码优先

c# - 通过路径设置JSON属性

sql - 无法绑定(bind)多部分标识符 "i.charged_amount"

mysql - 在数据库中存储 100K 个不同的表

SQL Server : Do I need to use GO statements between batches?