C
C#16mo ago
pirrippu

✅ SQL Command Timeout does not work with async calls

Has anyone encountered the following issue? When using a sync call to the database, the command timeout parameter works, but when I'm using an async call it does not work? See sample code below:
var connection = new MySqlConnection("***");await connection.OpenAsync();
var command = new MySqlCommand("select Count(Id) from eventlog", connection);
command.CommandTimeout = 10;

// Hits the exception at 10 second mark
var count = command.ExecuteScalar();

// Executes indefinitely
var count = await command.ExecuteScalarAsync(cancellationToken);
var connection = new MySqlConnection("***");await connection.OpenAsync();
var command = new MySqlCommand("select Count(Id) from eventlog", connection);
command.CommandTimeout = 10;

// Hits the exception at 10 second mark
var count = command.ExecuteScalar();

// Executes indefinitely
var count = await command.ExecuteScalarAsync(cancellationToken);
Removing the CommandTimeout, meaning using the default timeout of 30 seconds, works for sync calls but not with async calls. I tried adding a connection timeout and it also does not work. I tried using Dapper, and it also does not work. Am I missing something or what?
5 Replies
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
pirrippu
pirrippuOP16mo ago
Cheesus crust. CommandTimeout and CancellationTokens are indeed working.
The problem is with MySql.Data, I tried using Pomelo as some suggested in other forums. But going back to your reply, yes, I do have cancellationToken and a command timeout. The problem was that if my client didn't configure a proper timeout, the query will run indefinitely because the cancellation does not work. So the command timeout is there as a final catch for this kind of scenario. And do note that this query is expected to run for more than few seconds due to the nature of the schema and that the query is there for the sake of example. I'll open an issue with MySql.Data. And use Pomelo nuget instead.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
pirrippu
pirrippuOP16mo ago
Yes, thanks for this, I considered your proposal and it looks better now. I also did combine CancellationTokens from the controller and the database request.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server