I have the following code:
public static async Task<uint?> GetJobAsync(string name)
{
return await Program.Database.PerformQueryAsync<uint?>(async (command) =>
{
command.CommandText = "SELECT `job` FROM `people` WHERE `name`=@name;";
command.Parameters.Add(new MySqlParameter("name", name));
MySqlDataReader reader = await command.ExecuteReaderAsync() as MySqlDataReader;
if (!reader.HasRows)
return null;
reader.Read();
return reader.GetUInt32(0);
});
}
public async Task<T> PerformQueryAsync<T>(Func<MySqlCommand, Task<T>> operationAsync)
{
try
{
using (MySqlCommand command = CreateCommand())
return await operationAsync(command);
}
catch (MySqlException ex)
{
if (ex.GetBaseException() is SocketException)
{
Console.WriteLine("MySQL connection was broken. Reconnecting...");
if (!IsConnected())
Connect();
using (MySqlCommand command = CreateCommand())
return await operationAsync(command);
}
throw;
}
}
The second function exists as the underlying MySQL connection often wait_timeouts. My SQL query is short and takes less time. However, 3 async methods are called to fetch this information. Is this a good use case of async? Should I convert to synchronous code?
My concerns arise from the suggestion in this Microsoft magazine article showing the Il code generated for a simple async call.