Improve Connection / Disconnect behavior

This commit is contained in:
2020-04-08 11:13:08 +02:00
parent 61966d0c1f
commit 2365589910
4 changed files with 97 additions and 43 deletions
+10
View File
@@ -28,6 +28,16 @@ void MySQLClient::Disconnect()
m_pMySQLClientImpl->Disconnect(); m_pMySQLClientImpl->Disconnect();
} }
void MySQLClient::Reconnect()
{
m_pMySQLClientImpl->Reconnect();
}
bool MySQLClient::Connected()
{
return m_pMySQLClientImpl->Connected();
}
std::string MySQLClient::Database() const std::string MySQLClient::Database() const
{ {
return m_pMySQLClientImpl->Database(); return m_pMySQLClientImpl->Database();
+68 -39
View File
@@ -19,55 +19,40 @@ 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)
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
try m_hostname = hostname;
{ m_username = username;
if (m_pConnection) m_password = password;
Disconnect();
std::stringstream ss;
ss << "tcp://" << hostname << ":3306";
m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), username, password));
m_database = ""; m_database = "";
m_connected = true; InternalConnect();
}
catch (sql::SQLException &e)
{
std::stringstream ss;
ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
} }
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)
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
try m_hostname = hostname;
{ m_username = username;
if (m_pConnection) m_password = password;
Disconnect();
std::stringstream ss;
ss << "tcp://" << hostname << ":3306/" << database;
m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), username, password));
m_database = database; m_database = database;
m_connected = true; InternalConnect();
}
catch (sql::SQLException &e)
{
std::stringstream ss;
ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
} }
void MySQLClientImpl::Disconnect() void MySQLClientImpl::Disconnect()
{ {
std::unique_lock<std::mutex> lock(m_mutex); std::unique_lock<std::mutex> lock(m_mutex);
m_pConnection.reset(); InternalDisconnect();
m_database = ""; }
m_connected = false;
void MySQLClientImpl::Reconnect()
{
std::unique_lock<std::mutex> lock(m_mutex);
InternalDisconnect();
InternalConnect();
}
bool MySQLClientImpl::Connected() const
{
std::unique_lock<std::mutex> lock(m_mutex);
return m_connected;
} }
std::string MySQLClientImpl::Database() const std::string MySQLClientImpl::Database() const
@@ -77,7 +62,7 @@ std::string MySQLClientImpl::Database() const
return m_database; return m_database;
} }
void MySQLClientImpl::Execute(const std::string& query) const 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)
@@ -93,11 +78,17 @@ void MySQLClientImpl::Execute(const std::string& query) const
std::stringstream ss; std::stringstream ss;
ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl; ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl;
ss << "Query: " << query << std::endl; ss << "Query: " << query << std::endl;
ss << "(MySQL error code: " << e.getErrorCode();
ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
int errorCode = e.getErrorCode();
if (errorCode == 2006 || errorCode == 2013)
InternalDisconnect();
} }
} }
std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::string& query) const std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(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)
@@ -114,10 +105,48 @@ std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
std::stringstream ss; std::stringstream ss;
ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl; ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl;
ss << "Query: " << query << std::endl; ss << "Query: " << query << std::endl;
ss << "(MySQL error code: " << e.getErrorCode();
ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
Logging::Log(Logging::Severity::Error, ss.str()); Logging::Log(Logging::Severity::Error, ss.str());
int errorCode = e.getErrorCode();
if (errorCode == 2006 || errorCode == 2013)
InternalDisconnect();
} }
return std::make_shared<MySQLResultSetImpl>(nullptr); return std::make_shared<MySQLResultSetImpl>(nullptr);
} }
void MySQLClientImpl::InternalConnect()
{
try
{
if (m_pConnection)
Disconnect();
std::stringstream ss;
ss << "tcp://" << m_hostname << ":3306";
if (!m_database.empty())
ss << "/" << m_database;
m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), m_username, m_password));
m_connected = true;
}
catch (sql::SQLException &e)
{
std::stringstream ss;
ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
InternalDisconnect();
}
}
void MySQLClientImpl::InternalDisconnect()
{
m_pConnection.reset();
m_database = "";
m_connected = false;
}
} // namespace MySQL } // namespace MySQL
+14 -2
View File
@@ -22,17 +22,29 @@ public:
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);
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);
void Disconnect(); void Disconnect();
void Reconnect();
bool Connected() const;
std::string Database() const; std::string Database() const;
void Execute(const std::string& query) const; void Execute(const std::string& query);
std::shared_ptr<MySQLResultSetImpl> ExecuteQuery(const std::string& query) const; std::shared_ptr<MySQLResultSetImpl> ExecuteQuery(const std::string& query);
private:
void InternalConnect();
void InternalDisconnect();
private: private:
mutable std::mutex m_mutex; mutable std::mutex m_mutex;
bool m_connected; bool m_connected;
std::string m_hostname;
std::string m_username;
std::string m_password;
std::string m_database; std::string m_database;
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;
}; };
+3
View File
@@ -18,7 +18,10 @@ public:
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);
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);
void Disconnect(); void Disconnect();
void Reconnect();
bool Connected();
std::string Database() const; std::string Database() const;