我有这个方法,它在多个连接上的mysql数据库上执行sql命令。
public async Task<long[][]> ExecuteCommand(string sqlCommand, int tries, int concurrent,
CancellationToken cancellationToken)
{
EnsureConnections(concurrent);
//This line takes about 2 seconds before hitting the log method when using mysql, postgres and sqlserver execute this line instantly and block at Task.WhenAll
var tasks = Connections
.Select(connection => connection.ExecuteQuery(sqlCommand, tries, cancellationToken))
.ToList();
Log.Debug("Completed gathering tasks");
return await Task.WhenAll(tasks);
}
这是connection.executeQuery:
public override async Task<long[]> ExecuteQuery(string sqlCommand, int tries,
CancellationToken cancellationToken)
{
IsExecuting = true;
var times = await FunctionTimer.GetOperationTimesAsync(async () =>
{
using (var command =
new MySqlCommand(sqlCommand, BaseConnection as MySql.Data.MySqlClient.MySqlConnection))
{
command.CommandTimeout = 0;
await command.ExecuteNonQueryAsync(cancellationToken);
}
}, tries, cancellationToken);
IsExecuting = false;
return times;
}
正如您所看到的,ExecuteQuery方法是异步的,而ExecuteNonQueryAsync也是异步的。 奇怪的是我也为Postgres和SQL Server设置了这个设置,它们都应该阻止它们(在Task.WhenAll(任务))。当我只将任务放在列表中时,为什么mysql版本会阻塞?