【问题标题】:Scoped DataContext intermittently raises ExecuteReader errorScoped DataContext 间歇性引发 ExecuteReader 错误
【发布时间】:2009-07-17 15:31:53
【问题描述】:

我们的应用程序遵循为每个线程/HttpContext 维护一个 DataContext 的方法,使用 Rick Strahl 在其blog 中概述的 DataContextFactory 类,包括对 Richard 提到的 Key 的修改(使用 type.AssemblyQualifiedName)。

该解决方案看起来不错(尽管在大多数情况下,不同的方法可能会更好),但是在使用此解决方案时,我看到应用程序中出现间歇性错误:

ExecuteReader 需要一个打开且 可用的连接。连接的 当前状态已关闭。

我使用以下属性访问整个代码中的数据上下文,这是我的 DataContext 类的一部分:

/// <summary>
/// Returns the current datacontext for the thread or HttpContext, creating one if it does not exist.
/// </summary>
public static SharedDataContext Current
{
    get
    {
        return DataContextFactory.GetScopedDataContext<SharedDataContext>();
    }
}

由于是间歇性的,我很难确定这一点。有谁知道我可能做错了什么,或者知道如何调试这个问题?


请注意,这可能与 unanswered 问题 here

重复

【问题讨论】:

    标签: c# multithreading linq-to-sql datacontext


    【解决方案1】:

    我们现在使用自定义 DataContextFactory 来维护每个事务的数据上下文。唯一的其他主要变化是对属性使用 [ThreadStatic],而不是通过 Thread.Get/SetData 手动访问

    由于此更改似乎不再出现问题,并且没有提出其他答案,我建议 [ThreadStatic] 作为解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 2016-05-20
      • 1970-01-01
      • 2023-03-07
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多