Add Auto Reconnect
This commit is contained in:
@@ -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()
|
void MySQLClient::Disconnect()
|
||||||
@@ -28,9 +28,9 @@ void MySQLClient::Disconnect()
|
|||||||
m_pMySQLClientImpl->Disconnect();
|
m_pMySQLClientImpl->Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySQLClient::Reconnect()
|
void MySQLClient::Reconnect(bool automaticReconnect)
|
||||||
{
|
{
|
||||||
m_pMySQLClientImpl->Reconnect();
|
m_pMySQLClientImpl->Reconnect(automaticReconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MySQLClient::Connected()
|
bool MySQLClient::Connected()
|
||||||
|
|||||||
+21
-13
@@ -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<std::mutex> lock(m_mutex);
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
m_hostname = hostname;
|
m_hostname = hostname;
|
||||||
m_username = username;
|
m_username = username;
|
||||||
m_password = password;
|
m_password = password;
|
||||||
m_database = "";
|
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<std::mutex> lock(m_mutex);
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
m_hostname = hostname;
|
m_hostname = hostname;
|
||||||
m_username = username;
|
m_username = username;
|
||||||
m_password = password;
|
m_password = password;
|
||||||
m_database = database;
|
m_database = database;
|
||||||
InternalConnect();
|
InternalConnect(automaticReconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySQLClientImpl::Disconnect()
|
void MySQLClientImpl::Disconnect()
|
||||||
@@ -42,11 +42,11 @@ void MySQLClientImpl::Disconnect()
|
|||||||
InternalDisconnect();
|
InternalDisconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySQLClientImpl::Reconnect()
|
void MySQLClientImpl::Reconnect(bool automaticReconnect)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(m_mutex);
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
InternalDisconnect();
|
InternalDisconnect();
|
||||||
InternalConnect();
|
InternalConnect(automaticReconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MySQLClientImpl::Connected() const
|
bool MySQLClientImpl::Connected() const
|
||||||
@@ -117,20 +117,28 @@ std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
|
|||||||
return std::make_shared<MySQLResultSetImpl>(nullptr);
|
return std::make_shared<MySQLResultSetImpl>(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySQLClientImpl::InternalConnect()
|
void MySQLClientImpl::InternalConnect(bool automaticReconnect)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (m_pConnection)
|
if (m_pConnection)
|
||||||
Disconnect();
|
InternalDisconnect();
|
||||||
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << "tcp://" << m_hostname << ":3306";
|
|
||||||
|
|
||||||
|
std::string database;
|
||||||
if (!m_database.empty())
|
if (!m_database.empty())
|
||||||
ss << "/" << m_database;
|
database = m_database;
|
||||||
|
else
|
||||||
|
database = "information_schema";
|
||||||
|
|
||||||
m_pConnection = std::shared_ptr<sql::Connection>(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<sql::Connection>(m_pDriver->connect(options));
|
||||||
m_connected = true;
|
m_connected = true;
|
||||||
}
|
}
|
||||||
catch (sql::SQLException &e)
|
catch (sql::SQLException &e)
|
||||||
|
|||||||
@@ -20,11 +20,11 @@ public:
|
|||||||
MySQLClientImpl();
|
MySQLClientImpl();
|
||||||
~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, bool automaticReconnect);
|
||||||
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, const std::string& database, bool automaticReconnect);
|
||||||
|
|
||||||
void Disconnect();
|
void Disconnect();
|
||||||
void Reconnect();
|
void Reconnect(bool automaticReconnect);
|
||||||
bool Connected() const;
|
bool Connected() const;
|
||||||
|
|
||||||
std::string Database() const;
|
std::string Database() const;
|
||||||
@@ -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();
|
void InternalConnect(bool automaticReconnect);
|
||||||
void InternalDisconnect();
|
void InternalDisconnect();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ public:
|
|||||||
MySQLClient();
|
MySQLClient();
|
||||||
~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, bool automaticReconnect = true);
|
||||||
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, const std::string& database, bool automaticReconnect = true);
|
||||||
|
|
||||||
void Disconnect();
|
void Disconnect();
|
||||||
void Reconnect();
|
void Reconnect(bool automaticReconnect = true);
|
||||||
bool Connected();
|
bool Connected();
|
||||||
|
|
||||||
std::string Database() const;
|
std::string Database() const;
|
||||||
|
|||||||
Reference in New Issue
Block a user