Automatically reconnect when initial connections fails

This commit is contained in:
2020-07-06 13:49:19 +02:00
parent 423fff414b
commit 5084df6f7c
2 changed files with 23 additions and 8 deletions
+21 -7
View File
@@ -8,6 +8,7 @@ namespace MySQL {
MySQLClientImpl::MySQLClientImpl() : MySQLClientImpl::MySQLClientImpl() :
m_connected(false), m_connected(false),
m_automaticReconnect(false),
m_pDriver(sql::mysql::get_mysql_driver_instance()) 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_username = username;
m_password = password; m_password = password;
m_database = ""; 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) 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_username = username;
m_password = password; m_password = password;
m_database = database; m_database = database;
InternalConnect(automaticReconnect); m_automaticReconnect = automaticReconnect;
InternalConnect();
} }
void MySQLClientImpl::Disconnect() void MySQLClientImpl::Disconnect()
@@ -46,7 +49,8 @@ void MySQLClientImpl::Reconnect(bool automaticReconnect)
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
InternalDisconnect(); InternalDisconnect();
InternalConnect(automaticReconnect); m_automaticReconnect = automaticReconnect;
InternalConnect();
} }
bool MySQLClientImpl::Connected() const bool MySQLClientImpl::Connected() const
@@ -66,7 +70,12 @@ void MySQLClientImpl::Execute(const std::string& query)
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
if (!m_connected) 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 try
{ {
@@ -92,7 +101,12 @@ std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
if (!m_connected) 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 try
{ {
@@ -117,7 +131,7 @@ std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
return std::make_shared<MySQLResultSetImpl>(nullptr); return std::make_shared<MySQLResultSetImpl>(nullptr);
} }
void MySQLClientImpl::InternalConnect(bool automaticReconnect) void MySQLClientImpl::InternalConnect()
{ {
try try
{ {
@@ -136,7 +150,7 @@ void MySQLClientImpl::InternalConnect(bool automaticReconnect)
options["password"] = m_password; options["password"] = m_password;
options["schema"] = database; options["schema"] = database;
options["port"] = 3306; options["port"] = 3306;
options["OPT_RECONNECT"] = automaticReconnect; options["OPT_RECONNECT"] = m_automaticReconnect;
m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(options)); m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(options));
m_connected = true; m_connected = true;
+2 -1
View File
@@ -33,7 +33,7 @@ public:
std::shared_ptr<MySQLResultSetImpl> ExecuteQuery(const std::string& query); std::shared_ptr<MySQLResultSetImpl> ExecuteQuery(const std::string& query);
private: private:
void InternalConnect(bool automaticReconnect); void InternalConnect();
void InternalDisconnect(); void InternalDisconnect();
private: private:
@@ -44,6 +44,7 @@ private:
std::string m_username; std::string m_username;
std::string m_password; std::string m_password;
std::string m_database; std::string m_database;
bool m_automaticReconnect;
sql::mysql::MySQL_Driver* m_pDriver; sql::mysql::MySQL_Driver* m_pDriver;
std::shared_ptr<sql::Connection> m_pConnection; std::shared_ptr<sql::Connection> m_pConnection;