private async Task <IFetchedMessage> FetchNextMessageCoreAsync(string sql, object args = null) { //here don't use `using` to dispose var connection = new MySqlConnection(_options.ConnectionString); await connection.OpenAsync(); var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); FetchedMessage fetchedMessage = null; try { fetchedMessage = await connection.QueryFirstOrDefaultAsync <FetchedMessage>(sql, args, transaction); } catch (MySqlException) { transaction.Dispose(); throw; } if (fetchedMessage == null) { transaction.Rollback(); transaction.Dispose(); connection.Dispose(); return(null); } return(new MySqlFetchedMessage(fetchedMessage.MessageId, fetchedMessage.MessageType, connection, transaction)); }
private async Task <IFetchedMessage> FetchNextMessageCoreAsync(string sql, object args = null) { //here don't use `using` to dispose var connection = new MySqlConnection(Options.ConnectionString); await connection.OpenAsync(); var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted); FetchedMessage fetchedMessage = null; try { //fetchedMessage = await connection.QuerySingleOrDefaultAsync<FetchedMessage>(sql, args, transaction); // An anomaly with unknown causes, sometimes QuerySingleOrDefaultAsync can't return expected result. using (var reader = connection.ExecuteReader(sql, args, transaction)) { while (reader.Read()) { fetchedMessage = new FetchedMessage { MessageId = (int)reader.GetInt64(0), MessageType = (MessageType)reader.GetInt64(1) }; } } } catch (MySqlException) { transaction.Dispose(); throw; } if (fetchedMessage == null) { transaction.Rollback(); transaction.Dispose(); connection.Dispose(); return(null); } return(new MySqlFetchedMessage(fetchedMessage.MessageId, fetchedMessage.MessageType, connection, transaction)); }