MySqlConnector
MySqlConnector copied to clipboard
keepaliveTime - to keep the min pool size alive after wait_timeout
Current
The connections will be closed after wait_timeout in server side, "Min Pool Size" dot not work.
Expect
Set a property to keep alive.
HikariCP used a property keepaliveTime to solve that.
MySqlConnector does not currently attempt to "circumvent" the server's wait_timeout setting by artificially keeping client threads busy (e.g., by pinging the server).
We recommend increasing wait_timeout to the appropriate time for your application's needs.
If, for some reason, that can't be done, you can simulate it in your code with something like this:
// get number of idle connections to ping
var builder = new MySqlConnectionStringBuilder(connectionString);
var connectionsToPing = builder.MinimumPoolSize;
// keep pinging to keep the connections alive
while (true)
{
// ping all connections
var connections = new List<MySqlConnection>();
for (var i = 0; i < connectionsToPing; i++)
{
var connection = new MySqlConnection(connectionString);
connection.Open();
connection.Ping();
connections.Add(connection);
}
// keep them all open so the same connection isn't reused
foreach (var connection in connections)
connection.Dispose();
// delay before pinging them all again
Thread.Sleep(TimeSpan.FromSeconds(30));
}
Thanks @bgrainger. Is there any roadmap to add some feature like this?
There is not. Leaving this issue open to collect user interest.
We recommend increasing wait_timeout to the appropriate time for your application's needs.
It's not always possible. For example, Azure MySQL Single Server has max value of wait_timeout of 240.
Azure MySQL Single Server
That product is also scheduled for retirement by September 16, 2024. 😀
But point taken.