mysql-net / MySqlConnector

MySQL Connector for .NET
https://mysqlconnector.net
MIT License
1.38k stars 330 forks source link

keepaliveTime - to keep the min pool size alive after wait_timeout #1225

Open chinaq opened 1 year ago

chinaq commented 1 year ago

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.

bgrainger commented 1 year ago

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));
}
chinaq commented 1 year ago

Thanks @bgrainger. Is there any roadmap to add some feature like this?

bgrainger commented 1 year ago

There is not. Leaving this issue open to collect user interest.

stilettk commented 1 year ago

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.

bgrainger commented 1 year ago

Azure MySQL Single Server

That product is also scheduled for retirement by September 16, 2024. 😀

But point taken.