From a45717eaf6f6e1ea63308250bc11ae3728b5f7b4 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Wed, 8 Apr 2020 12:41:10 +0200 Subject: [PATCH] Add Auto Reconnect --- MySQL/MySQLClient.cpp | 12 ++++++------ MySQL/MySQLClientImpl.cpp | 34 +++++++++++++++++++++------------- MySQL/MySQLClientImpl.h | 8 ++++---- include/MySQLClient.h | 6 +++--- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/MySQL/MySQLClient.cpp b/MySQL/MySQLClient.cpp index 31bf681..cf899f4 100644 --- a/MySQL/MySQLClient.cpp +++ b/MySQL/MySQLClient.cpp @@ -13,14 +13,14 @@ MySQLClient::~MySQLClient() { } -void MySQLClient::Connect(const std::string& hostname, const std::string& username, const std::string& password) +void MySQLClient::Connect(const std::string& hostname, const std::string& username, const std::string& password, bool automaticReconnect) { - m_pMySQLClientImpl->Connect(hostname, username, password); + m_pMySQLClientImpl->Connect(hostname, username, password, automaticReconnect); } -void MySQLClient::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database) +void MySQLClient::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database, bool automaticReconnect) { - m_pMySQLClientImpl->Connect(hostname, username, password, database); + m_pMySQLClientImpl->Connect(hostname, username, password, database, automaticReconnect); } void MySQLClient::Disconnect() @@ -28,9 +28,9 @@ void MySQLClient::Disconnect() m_pMySQLClientImpl->Disconnect(); } -void MySQLClient::Reconnect() +void MySQLClient::Reconnect(bool automaticReconnect) { - m_pMySQLClientImpl->Reconnect(); + m_pMySQLClientImpl->Reconnect(automaticReconnect); } bool MySQLClient::Connected() diff --git a/MySQL/MySQLClientImpl.cpp b/MySQL/MySQLClientImpl.cpp index 2e516fb..73b4d3d 100644 --- a/MySQL/MySQLClientImpl.cpp +++ b/MySQL/MySQLClientImpl.cpp @@ -16,24 +16,24 @@ MySQLClientImpl::~MySQLClientImpl() { } -void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password) +void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, bool automaticReconnect) { std::unique_lock lock(m_mutex); m_hostname = hostname; m_username = username; m_password = password; m_database = ""; - InternalConnect(); + InternalConnect(automaticReconnect); } -void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database) +void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database, bool automaticReconnect) { std::unique_lock lock(m_mutex); m_hostname = hostname; m_username = username; m_password = password; m_database = database; - InternalConnect(); + InternalConnect(automaticReconnect); } void MySQLClientImpl::Disconnect() @@ -42,11 +42,11 @@ void MySQLClientImpl::Disconnect() InternalDisconnect(); } -void MySQLClientImpl::Reconnect() +void MySQLClientImpl::Reconnect(bool automaticReconnect) { std::unique_lock lock(m_mutex); InternalDisconnect(); - InternalConnect(); + InternalConnect(automaticReconnect); } bool MySQLClientImpl::Connected() const @@ -117,20 +117,28 @@ std::shared_ptr MySQLClientImpl::ExecuteQuery(const std::str return std::make_shared(nullptr); } -void MySQLClientImpl::InternalConnect() +void MySQLClientImpl::InternalConnect(bool automaticReconnect) { try { if (m_pConnection) - Disconnect(); - - std::stringstream ss; - ss << "tcp://" << m_hostname << ":3306"; + InternalDisconnect(); + std::string database; if (!m_database.empty()) - ss << "/" << m_database; + database = m_database; + else + database = "information_schema"; - m_pConnection = std::shared_ptr(m_pDriver->connect(ss.str(), m_username, m_password)); + sql::ConnectOptionsMap options; + options["hostName"] = m_hostname; + options["userName"] = m_username; + options["password"] = m_password; + options["schema"] = database; + options["port"] = 3306; + options["OPT_RECONNECT"] = automaticReconnect; + + m_pConnection = std::shared_ptr(m_pDriver->connect(options)); m_connected = true; } catch (sql::SQLException &e) diff --git a/MySQL/MySQLClientImpl.h b/MySQL/MySQLClientImpl.h index 434f032..12354f6 100644 --- a/MySQL/MySQLClientImpl.h +++ b/MySQL/MySQLClientImpl.h @@ -20,11 +20,11 @@ public: MySQLClientImpl(); ~MySQLClientImpl(); - void Connect(const std::string& hostname, const std::string& username, const std::string& password); - void Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database); + void Connect(const std::string& hostname, const std::string& username, const std::string& password, bool automaticReconnect); + void Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database, bool automaticReconnect); void Disconnect(); - void Reconnect(); + void Reconnect(bool automaticReconnect); bool Connected() const; std::string Database() const; @@ -33,7 +33,7 @@ public: std::shared_ptr ExecuteQuery(const std::string& query); private: - void InternalConnect(); + void InternalConnect(bool automaticReconnect); void InternalDisconnect(); private: diff --git a/include/MySQLClient.h b/include/MySQLClient.h index 0e43292..edeb500 100644 --- a/include/MySQLClient.h +++ b/include/MySQLClient.h @@ -16,11 +16,11 @@ public: MySQLClient(); ~MySQLClient(); - void Connect(const std::string& hostname, const std::string& username, const std::string& password); - void Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database); + void Connect(const std::string& hostname, const std::string& username, const std::string& password, bool automaticReconnect = true); + void Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database, bool automaticReconnect = true); void Disconnect(); - void Reconnect(); + void Reconnect(bool automaticReconnect = true); bool Connected(); std::string Database() const;