Skip to content

Instantly share code, notes, and snippets.

@bgrainger
Last active October 28, 2022 23:22
Show Gist options
  • Save bgrainger/7cc049f163d64f4b71d6e666fd30a954 to your computer and use it in GitHub Desktop.
Save bgrainger/7cc049f163d64f4b71d6e666fd30a954 to your computer and use it in GitHub Desktop.
version: '2'
services:
master:
image: mariadb:10.8
environment:
MYSQL_ROOT_PASSWORD: 'pass'
command: mysqld --max-allowed-packet=96M --character-set-server=utf8mb4 --max-connections=250
ports:
- "3306:3306"
maxscale:
image: mariadb/maxscale:2.5
depends_on:
- master
volumes:
- ./maxscale.cnf.d:/etc/maxscale.cnf.d
ports:
- "4006:4006" # readwrite port
- "8989:8989" # REST API port
[server1]
type=server
address=master
port=3306
protocol=MariaDBBackend
[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1
user=root
password=pass
failcount=3
backend_connect_timeout=3
backend_write_timeout=3
backend_read_timeout=3
auto_failover=true
auto_rejoin=true
enforce_read_only_slaves=1
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1
user=root
password=pass
master_failure_mode=fail_on_write
enable_root_user=true
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MySQLClient
port=4006
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="MySqlConnector" Version="2.1.13" />
</ItemGroup>
</Project>
using Dapper;
using MySqlConnector;
await MethodToRunAsync();
async Task Method1Async()
{
using (var domain = new MySqlConnection())
{
await domain.OpenDBAsync();
await domain.QueryAsync(@"SELECT * FROM mysql.user LIMIT 1");
}
}
async Task Method2Async()
{
using (var domain = new MySqlConnection())
{
Console.WriteLine("Before open");
await domain.OpenDBAsync(); // Code blocks here after running Method1Async()
Console.WriteLine("After open");
using (var trans = await domain.BeginTransactionAsync())
{
await domain.ExecuteAsync("SELECT 1;", transaction: trans);
await trans.RollbackAsync();
}
}
Console.WriteLine("End of Method2");
}
async Task MethodToRunAsync()
{
await Method1Async();
await Method2Async(); // Blocks Here
}
public static class ExtensionMethods
{
public static async Task OpenDBAsync(this MySqlConnection conn, bool allowUserVariables = false)
{
conn.ConnectionString = "server=127.0.0.1;port=4006;user=root;password=pass";
await conn.OpenAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment