【问题标题】:MVC 3/EF/SQL Server strange connection timeout issueMVC 3/EF/SQL Server 奇怪的连接超时问题
【发布时间】:2012-02-22 12:58:48
【问题描述】:

在阅读之前请注意,我已经在 Google 上搜索过这个并阅读了大量关于 SO 和其他地方的文章,目前还没有任何建议有效。

我在登录我的 MVC 3 应用程序时随机收到一个超时错误。它发生的时间超过一半 - 应用程序登录的所有其他时间都很好。当它不起作用时,它会尝试大约 10 秒然后出错。

错误:

异常:“执行命令定义时发生错误。有关详细信息,请参阅内部异常。” 内部异常:{“超时已过期。在操作完成之前超时时间已过或服务器未响应。\r\n超时已过期。在操作完成之前已超时或服务器未响应。”}

这发生在我的存储库类中,它直接与实体框架交互。


登录并简单地从数据库中提取快速检查时似乎会发生这种情况,例如:

 return entities.Users.SingleOrDefault(user => user.UserName == userName);

 return (entities.Users.SingleOrDefault(u => u.UserId == user.UserId || u.UserName == user.UserName) != null);

我尝试过的事情:

  • SQL Server 验证
  • 集成安全性(我什至为每个可能的帐户提供了完整的数据库访问权限)
  • 在 IIS 之外运行
  • 在连接字符串中将连接超时设置得非常高(连接超时=50000)。 (我这里没有设置默认命令超时)
  • 在我的实体连接上将 CommandTimeout 设置为 0、5000、100000 等:entities.CommandTieout = 100000;
  • 在我使用存储库实例的每个 using 语句中设置 CommandTimeout。
  • 将 SingleOrDefault 翻转为 FirstOrDefault 等。
  • 启用/禁用延迟加载(为什么不呢?)

如果有帮助:

  • 我正在使用自定义角色和成员资格提供程序。
  • 我只是在 using 语句 (AccountRepository bleh = new AccountRepository()) 中从我的控制器进行调用,并且 AccountRepository 实现了 IDisposable 等。
  • 实体模型位于单独的项目中。
  • 我在 IIS 中运行该站点。它使用 4.0 集成应用程序池进行设置。
  • 所有帐户都拥有完整的数据库访问权限。
  • 当错误发生时,只要我在 web config 中设置(我认为是 50000)或存储库中的 commandtimeout,它就不会发生。
  • 它在登录时没有做太多,只是验证用户,获取用户角色然后加载一些少量数据,但是在登录时获取用户数据时总是出现错误。
  • 当我在调试之外尝试它时,它会重复错误四五次(自定义错误关闭)。

以下是事件日志中的完整异常:

Exception information: 
Exception type: SqlException 
Exception message: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
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.TdsParser.ThrowExceptionAndWarning()
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.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

【问题讨论】:

  • 数据库服务器相对于 Web 服务器在哪里,可能您遇到了网络问题
  • 您能否通过设置 SQL Server Profiler 从数据库服务器检查问题?
  • Ben - 我目前正在我的开发机器上直接运行 SQL Server 2008。 DOK - 我会检查一下……我对分析器不太熟悉,但会调查一下。
  • @AndroidTaco,如果您使用 Code First,请确保您的连接字符串名称属性与您的 DbContext 类名称相同。
  • 你可以通过谷歌搜索找到很多关于 SQL Profiler 的信息。这是一个包含视频的网站,可以帮助您入门sqlserverpedia.com/wiki/Using_SQL_Server_Profiler

标签: sql-server model-view-controller exception timeout repository


【解决方案1】:

您可以通过设置 SQL Server Profiler 从数据库服务器检查问题。

您可以通过谷歌搜索找到很多关于 SQL Profiler 的信息。这是site with a video that might help you get started.

【讨论】:

    【解决方案2】:

    编辑:虽然这确实对我有所帮助,但这不是解决方案。对于以后阅读的人来说,这个问题仍然存在。

    只是为了让大家知道 - 我相信我已经找到了问题所在。通过 SQL Profiler,我看到用于访问 SQL 的帐户实际上是本地系统帐户。然后我意识到,为了解决之前的问题,我已将 ASP.NET v4.0 应用程序池更改为使用本地系统帐户。我将身份更改回“ApplicationPoolIdentity”,将 IIS APPPOOL\ASP.NET v4.0 用户添加到数据库中,到目前为止一切正常。 @DOK - 非常感谢您提供有关 SQL Profiler 的信息,它帮助很大!也谢谢大家!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-06
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多