Automatically reconnect when initial connections fails
This commit is contained in:
@@ -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)
|
||||||
|
{
|
||||||
|
if (m_automaticReconnect)
|
||||||
|
InternalConnect();
|
||||||
|
else
|
||||||
throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server");
|
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)
|
||||||
|
{
|
||||||
|
if (m_automaticReconnect)
|
||||||
|
InternalConnect();
|
||||||
|
else
|
||||||
throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server");
|
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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user