【问题标题】:Changing a DataContext ConnectionTimeout does not affect the observable timeout更改 DataContext ConnectionTimeout 不会影响可观察到的超时
【发布时间】:2013-07-02 03:03:40
【问题描述】:

我有一个 System.Data.Linq.DataContext 对象,其 ConnectionTimeout 属性设置为 1 秒,但连接仍需要 30 秒才能返回异常。

这是显示问题的秒表输出代码:

Debug.WriteLine("!! Method start");
Stopwatch sw = Stopwatch.StartNew();

MyDataClassesDataContext dc = new MyDataClassesDataContext();

Debug.WriteLine("!! Connection.ConnectionTimeout is: " + dc.Connection.ConnectionTimeout);
// Output is: !! Connection.ConnectionTimeout is: 1

Debug.WriteLine("!! CommandTimeout is: " + dc.CommandTimeout);
// Output is: !! CommandTimeout is: 1

try
{
    string s = (from rows in dc.MyTable
                where rows.MyValue == 3
                select rows.MySecondValue).SingleOrDefault();

    return s;
}
catch (Exception)
{
    sw.Stop();
    Debug.WriteLine("!! Method ended after " + sw.Elapsed.Seconds + " seconds");
    // Output is: !! Method ended after 27 seconds

    return null;
}

ConnectionTimeout 属性使用连接字符串设置为 1。在连接之前它似乎是 1,所以它应该在 1 秒后超时,但需要 27 秒。这是使用 LINQ to SQL 时要修改的错误超时值吗?

【问题讨论】:

  • 我删除了我的答案,因为我认为这是 EF(我应该更多地查看标签)。你在哪里设置你的 DataContext 命令超时?
  • 我在 OnCreated 方法中执行此操作:partial void OnCreated() { this.CommandTimeout = 1; }
  • 异常是否与连接有关?因为那将是您需要更改的ConnectionTimeout(我知道这很明显,只需要知道)。
  • 是的,异常是 System.Data.SqlClient.SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)---> System.ComponentModel.Win32Exception (0x80004005):找不到网络路径
  • 您将CommandTimeout 设置为1,但没有发送任何命令。等待时间与ConnectionTimeout(我相信默认为30)有关。

标签: c# asp.net linq-to-sql


【解决方案1】:

ConnectionTimeoutCommandTimeout 之间存在差异,ConnectionTimeout 指定在放弃之前尝试连接到 Sql 服务器时等待多长时间,CommandTimeout 指定在之前等待命令/查询运行多长时间放弃。尝试设置CommandTimeouthttp://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.commandtimeout.aspx

【讨论】:

  • 我也尝试将 CommandTimeout 设置为 1(这是在发布的问题的初始版本中),但我删除了它,因为另一位评论者指出没有发送任何命令。
  • 为什么没有发送命令?您将如何让查询在服务器上执行?
  • 好的,有道理。我已将CommandTimeout 部分添加回示例代码中。
  • 那么,它有效吗?你能写出异常,而不是仅仅吞下它吗?它可能会帮助您了解正在发生的事情。
  • 不,它不起作用,仍然需要 27 秒。我在上一节的 cmets 中发布了异常。
猜你喜欢
  • 2018-03-17
  • 2017-09-21
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
相关资源
最近更新 更多