summaryrefslogtreecommitdiff
path: root/src/common/Database/MySQLConnection.cpp
diff options
context:
space:
mode:
authorFrancescoBorzi <borzifrancesco@gmail.com>2019-01-14 23:21:50 +0100
committerFrancescoBorzi <borzifrancesco@gmail.com>2019-01-14 23:21:50 +0100
commite5ad0165b36c62bb90fabea242427ba08f20695f (patch)
tree0b33a28aad63fb6f9f74718f69412515554eca04 /src/common/Database/MySQLConnection.cpp
parentc90afdd9e2b6604bb126b4e37821bc5b596957ad (diff)
Core/MySQL: wait for the DB to be ready
- Closes #1285
Diffstat (limited to 'src/common/Database/MySQLConnection.cpp')
-rw-r--r--src/common/Database/MySQLConnection.cpp78
1 files changed, 53 insertions, 25 deletions
diff --git a/src/common/Database/MySQLConnection.cpp b/src/common/Database/MySQLConnection.cpp
index 6b554ce8e7..4cbdfcfc4d 100644
--- a/src/common/Database/MySQLConnection.cpp
+++ b/src/common/Database/MySQLConnection.cpp
@@ -14,6 +14,8 @@
#include <mysql.h>
#include <mysqld_error.h>
#include <errmsg.h>
+#include <chrono>
+#include <thread>
#include "MySQLConnection.h"
#include "MySQLThreading.h"
@@ -24,6 +26,9 @@
#include "Timer.h"
#include "Log.h"
+using namespace std::this_thread;
+using namespace std::chrono;
+
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
m_reconnecting(false),
m_prepareError(false),
@@ -107,36 +112,59 @@ bool MySQLConnection::Open()
}
#endif
- m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(),
- m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0);
-
- if (m_Mysql)
- {
- if (!m_reconnecting)
+ // Possible improvement for future: make ATTEMPTS and SECONDS configurable values
+ uint const ATTEMPTS = 180;
+ uint const SECONDS = 10;
+
+ uint count = 0;
+ do {
+ m_Mysql = mysql_real_connect(
+ mysqlInit,
+ m_connectionInfo.host.c_str(),
+ m_connectionInfo.user.c_str(),
+ m_connectionInfo.password.c_str(),
+ m_connectionInfo.database.c_str(),
+ port,
+ unix_socket,
+ 0);
+
+ if (m_Mysql)
{
- sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info());
- sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql));
- // MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1
- // if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
- // sLog->outInfo(LOG_FILTER_SQL, "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
- }
+ if (!m_reconnecting)
+ {
+ sLog->outSQLDriver("MySQL client library: %s", mysql_get_client_info());
+ sLog->outSQLDriver("MySQL server ver: %s ", mysql_get_server_info(m_Mysql));
+ // MySQL version above 5.1 IS required in both client and server and there is no known issue with different versions above 5.1
+ // if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
+ // sLog->outInfo(LOG_FILTER_SQL, "[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
+ }
#if defined(ENABLE_EXTRAS) && defined(ENABLE_EXTRA_LOGS)
- sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
+ sLog->outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
#endif
- mysql_autocommit(m_Mysql, 1);
+ mysql_autocommit(m_Mysql, 1);
- // set connection properties to UTF8 to properly handle locales for different
- // server configs - core sends data in UTF8, so MySQL must expect UTF8 too
- mysql_set_character_set(m_Mysql, "utf8");
- return PrepareStatements();
- }
- else
- {
- sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
- mysql_close(mysqlInit);
- return false;
- }
+ // set connection properties to UTF8 to properly handle locales for different
+ // server configs - core sends data in UTF8, so MySQL must expect UTF8 too
+ mysql_set_character_set(m_Mysql, "utf8");
+ return PrepareStatements();
+ }
+ else
+ {
+ count++;
+ sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
+ sLog->outError("Retrying in 10 seconds...\n\n");
+ sleep_for(seconds(SECONDS));
+ }
+ } while (!m_Mysql && count < ATTEMPTS);
+
+ sLog->outError(
+ "Could not connect to MySQL database at %s: %s after %d attempts\n",
+ m_connectionInfo.host.c_str(),
+ mysql_error(mysqlInit),
+ ATTEMPTS);
+ mysql_close(mysqlInit);
+ return false;
}
bool MySQLConnection::PrepareStatements()