Frage

I have a DataContext set up using DBML and am using ExecuteMethodCall to stored procedures on a SQL Server database.

This is working fine but I've found that when I call two of the functions at the same time (via BackgroundWorkers) that the second thread is blocked until the first has completed.

Is there any way of running multiple ExecuteMethodCalls concurrently or does this require a separate DataContext?

Example Function Calls:

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));
    }

    // ...
}

The lockup occurs at the ExecuteMethodCall and control is not returned until the first thread has completed.

War es hilfreich?

Lösung

You cannot use a DataContext concurrently at all. The docs do not say that this is safe. All concurrent activity on an object is unsafe unless the docs state otherwise.

Use one DataContext per thread.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top