'Adding Keepalive option to MySQL connection causes error "Unable to connect to any of the specified MySQL hosts"
I am working on a project where a thread is run and opens a permanent database connection. The reason for this, is potentially as the project grows it could receive more and more requests so it's more efficient to keep the database open and usable instead of opening and closing the database.
Basically what the thread does is look for events in a queue, and if there is an event it starts working on the database to store and process the event. At its peak this thread could receive 50,000-100,000 requests a day potentially a lot more, as more and more users (hopefully) use the service.
However, because it's new, there are times where this thread doesn't have anything to do, so I end up hitting the exception "The connection must be valid and open" and I believe this is because the connection to the database is automatically dropped over 8 hours of inactivity. At the moment this can happen so I am trying to add a Keepalive option to the connection so this doesn't happen but for some reason, as soon as I add this, I then get the error "Unable to connect to any of the specified MySQL hosts".
I am using a MysqlConnectionStringBuilder as follows:
MySqlConnectionStringBuilder connectionBuilder = new MySqlConnectionStringBuilder();
connectionBuilder.Server = server;
connectionBuilder.UserID = username;
connectionBuilder.Password = password;
connectionBuilder.Port = 3306;
//Open the connection
MySqlConnection conn = new MySqlConnection(connectionBuilder.ConnectionString);
conn.Open();
This above works perfectly fine until I add the following:
connectionBuilder.Keepalive = 60;
When the above line is added is when I then get the error.
I am using a TCP connection as it's a remote connection from my Dev PC to a dev server - is there a setting on the server to enable this? as everything I've found on Google this option is all that's required.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
