Persistent Connections / Failover
Posted: Thu 18 Jul 2019 08:32
Hi,
I am getting started with dotConnect and am quite happy with the functionality. I am connecting to a Galera cluster with 3 servers, if one of them is unavailable, the connection should move to the next server.
At the moment this is done by using a local load balancer (pen) which does the job nicely, but I would like to move away from it as it is an additional tool I have to use.
My code for the initial evaluation was this:
In combination with an external load balancer this works like a charm. For production I needed a way to reset the error counter in case the connection was ok. As I have a lot of functions that query data I was not willing to reset the counter after every successful request, therefore I tried to play around with the StateChanged event:
After enabling this StateChange-Event on the connection object, I can see, that it opens and closes the connection for every query. As Timing is critical in this application, I want to keep the connection open as long as possible.
I tried setting the KeepAlive paramter in the connection string and also the PingInterval parameter in the connection object. Both settings did not change the behaviour.
Is there any way to disable this behaviour?
Bonus-Question: If I want to replace the external load balancer with the option to write multiple hosts in the connection string, how do I handle the switch to the next server? Do I need to do that in the ConnectionLost EventHandler?
Regards
Manuel
I am getting started with dotConnect and am quite happy with the functionality. I am connecting to a Galera cluster with 3 servers, if one of them is unavailable, the connection should move to the next server.
At the moment this is done by using a local load balancer (pen) which does the job nicely, but I would like to move away from it as it is an additional tool I have to use.
My code for the initial evaluation was this:
Code: Select all
public Database()
{
_conn = new MySqlConnection()
{
LocalFailover = true,
ConnectionString = GetConnectionString("ConnString"),
};
_conn.ConnectionLost += _conn_ConnectionLost;
}
private int errc = 0;
private void _conn_ConnectionLost(object sender, ConnectionLostEventArgs e)
{
if (errc++ > _maxretry)
{
e.RetryMode = RetryMode.Raise;
return;
}
if (e.Cause == ConnectionLostCause.Execute)
{
if (e.Context == ConnectionLostContext.None)
e.RetryMode = RetryMode.Reexecute;
else
e.RetryMode = RetryMode.Raise;
} else {
e.RetryMode = RetryMode.Raise;
}
}
Code: Select all
private void _conn_StateChange(object sender, StateChangeEventArgs e)
{
if (e.CurrentState.HasFlag(ConnectionState.Open)) errc = 0;
}
I tried setting the KeepAlive paramter in the connection string and also the PingInterval parameter in the connection object. Both settings did not change the behaviour.
Is there any way to disable this behaviour?
Bonus-Question: If I want to replace the external load balancer with the option to write multiple hosts in the connection string, how do I handle the switch to the next server? Do I need to do that in the ConnectionLost EventHandler?
Regards
Manuel