Skip to content

Instantly share code, notes, and snippets.

@nvivo
Created August 8, 2017 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvivo/3fc457baa29870c3395d46ecae4da2d2 to your computer and use it in GitHub Desktop.
Save nvivo/3fc457baa29870c3395d46ecae4da2d2 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dapper" Version="1.50.2" />
<PackageReference Include="mysqlconnector" Version="0.24.1" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "localhost",
UserID = "root",
Password = "",
Database = "information_schema",
MaximumPoolSize = 50,
ConnectionIdleTimeout = 10,
AllowUserVariables = false
};
var cs = builder.ConnectionString;
Task.Run(async () =>
{
while (true)
{
try
{
using (var cn = new MySqlConnection(cs))
{
await cn.OpenAsync();
await cn.QueryFirstAsync<int>("SET @test = 1; SELECT @test;");
}
}
catch
{ }
}
});
using (var cn = new MySqlConnection(cs))
{
cn.Open();
while (true)
{
var o = cn.QueryFirst("select max(time) as MaxSleepTime, count(*) as SleepingConnections from processlist where db = 'information_schema' and command = 'Sleep'");
Console.WriteLine($"Sleeping Connections: {(int) o.SleepingConnections}, Max Sleep Time: {(int) o.MaxSleepTime}");
Thread.Sleep(1000);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment