diff options
| author | Kargatum <dowlandtop@yandex.com> | 2021-04-12 15:09:13 +0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-12 10:09:13 +0200 |
| commit | 53ce87d0f7db456231b662d298d1c2cc7add37f9 (patch) | |
| tree | db16a8029c261262be3e078edc414ca70bf0bfb3 /src/common/Database/MySQLConnection.cpp | |
| parent | 81301c67d95a1e51bd269e8f4a49f373ecefeb42 (diff) | |
feat(Core/Database): implement db loader (#4431)
Diffstat (limited to 'src/common/Database/MySQLConnection.cpp')
| -rw-r--r-- | src/common/Database/MySQLConnection.cpp | 88 |
1 files changed, 37 insertions, 51 deletions
diff --git a/src/common/Database/MySQLConnection.cpp b/src/common/Database/MySQLConnection.cpp index d3b25fc2aa..06e9fe351c 100644 --- a/src/common/Database/MySQLConnection.cpp +++ b/src/common/Database/MySQLConnection.cpp @@ -47,12 +47,14 @@ MySQLConnection::MySQLConnection(ACE_Activation_Queue* queue, MySQLConnectionInf MySQLConnection::~MySQLConnection() { - ASSERT (m_Mysql); /// MySQL context must be present at this point + for (auto stmt : m_stmts) + delete stmt; - for (size_t i = 0; i < m_stmts.size(); ++i) - delete m_stmts[i]; - - mysql_close(m_Mysql); + if (m_Mysql) + { + mysql_close(m_Mysql); + m_Mysql = nullptr; + } } void MySQLConnection::Close() @@ -61,10 +63,9 @@ void MySQLConnection::Close() delete this; } -bool MySQLConnection::Open() +uint32 MySQLConnection::Open() { - MYSQL* mysqlInit; - mysqlInit = mysql_init(nullptr); + MYSQL* mysqlInit = mysql_init(nullptr); if (!mysqlInit) { sLog->outError("Could not initialize Mysql connection to database `%s`", m_connectionInfo.database.c_str()); @@ -106,59 +107,44 @@ bool MySQLConnection::Open() } #endif - // Possible improvement for future: make ATTEMPTS and SECONDS configurable values - uint32 const ATTEMPTS = 180; - - uint32 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) { - 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) { - 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)); + + if (mysql_get_server_version(m_Mysql) != mysql_get_client_version()) { - 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."); + sLog->outSQLDriver("[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 - { - 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"); - std::this_thread::sleep_for(10s); - } - } while (!m_Mysql && count < ATTEMPTS); + // 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 0; + } - sLog->outError( - "Could not connect to MySQL database at %s: %s after %d attempts\n", - m_connectionInfo.host.c_str(), - mysql_error(mysqlInit), - ATTEMPTS); + sLog->outError("Could not connect to MySQL database at %s: %s", m_connectionInfo.host.c_str(), mysql_error(mysqlInit)); + uint32 errorCode = mysql_errno(mysqlInit); mysql_close(mysqlInit); - return false; + return errorCode; } bool MySQLConnection::PrepareStatements() |
