【问题标题】:DataContext.ExecuteMethodCall from multiple threads来自多个线程的 DataContext.ExecuteMethodCall
【发布时间】:2014-04-09 13:38:32
【问题描述】:

我有一个使用 DBML 设置的 DataContext,并且正在使用 ExecuteMethodCall 来存储 SQL Server 数据库上的过程。

这工作正常,但我发现当我同时调用两个函数(通过 BackgroundWorkers)时,第二个线程被阻塞,直到第一个线程完成。

有什么方法可以同时运行多个 ExecuteMethodCalls 还是需要单独的 DataContext?

示例函数调用:

public class MyClass: MyDataContext
{
    public MyClass(string connection) : base(connection) 
    {  
    }

    // ...

    [Function(Name = "dbo.get_proposed")]  
    public ISingleResult<Order> get_proposed([Parameter(DbType = "Varchar(20)")] string region)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), region);
        return ((ISingleResult<Order>)(result.ReturnValue));
    }

    [Function(Name = "dbo.get_parent_groups")]
    public ISingleResult<Account> get_parent_groups([Parameter(DbType = "VarChar(40)")] string group_name)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), group_name);
        return ((ISingleResult<Account>)(result.ReturnValue));
    }

    // ...
}

在 ExecuteMethodCall 发生锁定,直到第一个线程完成后才返回控制。

【问题讨论】:

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


    【解决方案1】:

    您根本不能同时使用DataContext。文档并没有说这是安全的。除非文档另有说明,否则对象上的所有并发活动都是不安全的。

    每个线程使用一个DataContext

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      相关资源
      最近更新 更多