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
+19 -5
View File
@@ -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<std::mutex> 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<std::mutex> lock(m_mutex);
if (!m_connected)
{
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<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
{
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_connected)
{
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<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
return std::make_shared<MySQLResultSetImpl>(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<sql::Connection>(m_pDriver->connect(options));
m_connected = true;
+2 -1
View File
@@ -33,7 +33,7 @@ public:
std::shared_ptr<MySQLResultSetImpl> 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<sql::Connection> m_pConnection;