From 5084df6f7cd1ce13ede6e12ff4e0b0076a882bc7 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Mon, 6 Jul 2020 13:49:19 +0200 Subject: [PATCH] Automatically reconnect when initial connections fails --- MySQL/MySQLClientImpl.cpp | 28 +++++++++++++++++++++------- MySQL/MySQLClientImpl.h | 3 ++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/MySQL/MySQLClientImpl.cpp b/MySQL/MySQLClientImpl.cpp index 73b4d3d..7f338bc 100644 --- a/MySQL/MySQLClientImpl.cpp +++ b/MySQL/MySQLClientImpl.cpp @@ -8,6 +8,7 @@ namespace MySQL { MySQLClientImpl::MySQLClientImpl() : m_connected(false), + m_automaticReconnect(false), m_pDriver(sql::mysql::get_mysql_driver_instance()) { } @@ -23,7 +24,8 @@ void MySQLClientImpl::Connect(const std::string& hostname, const std::string& us m_username = username; m_password = password; m_database = ""; - InternalConnect(automaticReconnect); + m_automaticReconnect = automaticReconnect; + InternalConnect(); } void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database, bool automaticReconnect) @@ -33,7 +35,8 @@ void MySQLClientImpl::Connect(const std::string& hostname, const std::string& us m_username = username; m_password = password; m_database = database; - InternalConnect(automaticReconnect); + m_automaticReconnect = automaticReconnect; + InternalConnect(); } void MySQLClientImpl::Disconnect() @@ -46,7 +49,8 @@ void MySQLClientImpl::Reconnect(bool automaticReconnect) { std::unique_lock lock(m_mutex); InternalDisconnect(); - InternalConnect(automaticReconnect); + m_automaticReconnect = automaticReconnect; + InternalConnect(); } bool MySQLClientImpl::Connected() const @@ -66,7 +70,12 @@ void MySQLClientImpl::Execute(const std::string& query) { std::unique_lock lock(m_mutex); if (!m_connected) - throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server"); + { + if (m_automaticReconnect) + InternalConnect(); + else + throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server"); + } try { @@ -92,7 +101,12 @@ std::shared_ptr MySQLClientImpl::ExecuteQuery(const std::str { std::unique_lock lock(m_mutex); if (!m_connected) - throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server"); + { + if (m_automaticReconnect) + InternalConnect(); + else + throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server"); + } try { @@ -117,7 +131,7 @@ std::shared_ptr MySQLClientImpl::ExecuteQuery(const std::str return std::make_shared(nullptr); } -void MySQLClientImpl::InternalConnect(bool automaticReconnect) +void MySQLClientImpl::InternalConnect() { try { @@ -136,7 +150,7 @@ void MySQLClientImpl::InternalConnect(bool automaticReconnect) options["password"] = m_password; options["schema"] = database; options["port"] = 3306; - options["OPT_RECONNECT"] = automaticReconnect; + options["OPT_RECONNECT"] = m_automaticReconnect; m_pConnection = std::shared_ptr(m_pDriver->connect(options)); m_connected = true; diff --git a/MySQL/MySQLClientImpl.h b/MySQL/MySQLClientImpl.h index 12354f6..7603828 100644 --- a/MySQL/MySQLClientImpl.h +++ b/MySQL/MySQLClientImpl.h @@ -33,7 +33,7 @@ public: std::shared_ptr ExecuteQuery(const std::string& query); private: - void InternalConnect(bool automaticReconnect); + void InternalConnect(); void InternalDisconnect(); private: @@ -44,6 +44,7 @@ private: std::string m_username; std::string m_password; std::string m_database; + bool m_automaticReconnect; sql::mysql::MySQL_Driver* m_pDriver; std::shared_ptr m_pConnection;