Enable automatic reconnect in mysql (#1217)

Setting MYSQL_OPT_RECONNECT option to true will allow the connection to
be restablished once before giving up and returning an error.

This is useful in cases where the database is behind a loadbalancer
(e.g. ipvs) and the tcp connection is terminated after some period of
time.
This commit is contained in:
Omar Mohamed Khallaf 2022-04-22 03:12:49 +00:00 committed by GitHub
parent 567e7c07ad
commit d51bae1016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 0 deletions

View File

@ -56,6 +56,7 @@ MysqlConnection::MysqlConnection(trantor::EventLoop *loop,
{
mysql_init(mysqlPtr_.get());
mysql_options(mysqlPtr_.get(), MYSQL_OPT_NONBLOCK, nullptr);
mysql_optionsv(mysqlPtr_.get(), MYSQL_OPT_RECONNECT, &reconnect_);
// Get the key and value
auto connParams = parseConnString(connInfo);

View File

@ -110,6 +110,7 @@ class MysqlConnection : public DbConnection,
void startQuery();
void startStoreResult(bool queueInLoop);
int waitStatus_;
unsigned int reconnect_{1};
enum class ExecStatus
{
None = 0,