Skip to main content

How to Clear the MySql.Data Connection Pool

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Pooled connections stay open after you close them, usually for good reason. If you need them gone, MySqlConnection has two static methods. You'll rarely need them, so here's how they behave, and when you would.

The Program​

Save this as clear-pool.cs, in the folder with the tester from How to Write a MySQL Connection String for .NET C#. It prints MySQL's id for the connection after each step:

clear-pool.cs
#:package MySql.Data@26.10.0

using MySql.Data.MySqlClient;

var connectionString = Environment.GetEnvironmentVariable("MYSQL_CONNECTION_STRING")
?? throw new InvalidOperationException("Set the MYSQL_CONNECTION_STRING environment variable first.");

async Task<int> OpenAndCloseAsync()
{
await using var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
return connection.ServerThread;
}

Console.WriteLine($"First open: connection id {await OpenAndCloseAsync()}");
Console.WriteLine($"Second open: connection id {await OpenAndCloseAsync()}");

MySqlConnection.ClearAllPools();
Console.WriteLine($"After ClearAllPools: connection id {await OpenAndCloseAsync()}");

await using (var connection = new MySqlConnection(connectionString))
{
MySqlConnection.ClearPool(connection);
}
Console.WriteLine($"After ClearPool: connection id {await OpenAndCloseAsync()}");
Terminal
export MYSQL_CONNECTION_STRING="Server=127.0.0.1;Port=3306;Database=inventory;User ID=inventory_app;Password=$(grep '^MYSQL_PASSWORD=' ../dewiride-inventory-api/.env | cut -d= -f2-);SslMode=Required"
dotnet run clear-pool.cs
Output
First open: connection id 33
Second open: connection id 33
After ClearAllPools: connection id 34
After ClearPool: connection id 35

The clear-pool program printing the same connection id for two opens, a new id after ClearAllPools, and another after ClearPool.

Two opens share an id, as the pool intends. Each clear closed that connection, so the next open had to make a new one.

The Two Methods​

MethodClears
MySqlConnection.ClearPool(connection)The pool for that connection's connection string
MySqlConnection.ClearAllPools()Every pool in the process

For ClearPool, the connection only tells it which pool. It doesn't need to be open. Connections in use aren't cut off: they close when they're returned.

There are ClearPoolAsync and ClearAllPoolsAsync as well, but they're instance methods, unlike these two, so calling them on the class gives CS0120.

When You'd Want It​

Not often, and not routinely, because every request afterwards pays to open a connection:

  • After a failover, if you know about it, so connections move to the new server. Or set Connection Lifetime, and let it happen by itself.
  • After changing a password, to prove the new one works without waiting for a restart. Pooled connections keep working on the old one, and hide a mistake.
  • In a test, to start from nothing.

With EF Core, get the connection from the context: MySqlConnection.ClearPool((MySqlConnection)db.Database.GetDbConnection()).

Stale connections aren't a reason. When MySQL closed an idle one in my test, the driver noticed and made a new one, with no error.

Common Mistakes​

MistakeWhat happensDo this instead
Clearing on every requestYou've turned the pool off, the slow wayDon't
Using it to recover from errorsThe driver already replaces dead connectionsUse retries
Expecting busy connections to stopThey finish firstUse KILL for that

Troubleshooting​

SymptomLikely causeFix
CS0120 on ClearAllPoolsAsyncIt isn't staticThe fix
CS0176 on connection.ClearAllPools()It's staticCall it on the class
The readiness check's connections stayThat's a different poolUse ClearAllPools

Conclusion​

ClearPool empties one pool, and ClearAllPools empties them all. They're for rare moments, like a failover. Leave normal work to the pool.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.