Skip to content

Async Pipelined execution does not work with Microsoft.Data.SqlClient. #2028

@adameasten

Description

@adameasten

When migrating from System.Data.SqlClient to Microsoft.Data.SqlClient i stumbled on a strange behavior.
I'm using IDbConnection.ExecuteAsync(new CommandDefinition(...parameters: IEnumerable<T>, flags: CommandFlags.Pipelined)) on a few places in my code.
Works fine with System.Data.SqlClient but fails with Microsoft.Data.SqlClient. The exception i get is:

"System.InvalidOperationException: The method 'EndExecuteNonQuery' cannot be called more the once for the.."

I've looked in to the code, might found the problem, and tried a solution. The current state of the Task management when running async pipline mode with multiple parameters is described in short below. full version here

Foreach param, create a command and =>

var task = cmd.ExecuteNonQueryAsync(command.CancellationToken); 
pending.Enqueue(new AsyncExecState(cmd, task));

When creation of tasks based on multiple params is done =>

while (pending.Count != 0)
{
    var pair = pending.Dequeue();
    using (pair.Command) { /* dispose commands */ }
    total += await pair.Task.ConfigureAwait(false);
}

The exception seems to arise when the pair.Command is disposed and then the pair.Task is awaited.

What seems to fix the problem is to infer the using of the executing command with the corresponding work looking something like e.g:

Command creation based on multiple params =>

async Task<int> ExecuteQueryAsync(DbCommand cmd)
{
   using (cmd)
   {
       return await cmd.ExecuteNonQueryAsync(command.CancellationToken);
   }
}

var task = ExecuteQueryAsync(cmd);
pending.Enqueue(task);

Continuation =>

while (pending.Count != 0)
{
    var task = pending.Dequeue();
    total += await task.ConfigureAwait(false);
}

I'm to novice to get an overview on how this affects the management of the commands, and why it was splitted in the first place. I've tested it locally and the happy path works, but have not tested it extensivly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions