MySqlConnector icon indicating copy to clipboard operation
MySqlConnector copied to clipboard

keepaliveTime - to keep the min pool size alive after wait_timeout

Open chinaq opened this issue 3 years ago • 5 comments
trafficstars

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.

chinaq avatar Sep 27 '22 09:09 chinaq

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));
}

bgrainger avatar Sep 27 '22 20:09 bgrainger

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

chinaq avatar Sep 28 '22 01:09 chinaq

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

bgrainger avatar Sep 28 '22 11:09 bgrainger

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.

stilettk avatar Mar 24 '23 14:03 stilettk

Azure MySQL Single Server

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

But point taken.

bgrainger avatar Mar 24 '23 18:03 bgrainger