Ver Fonte

Improve Connection / Disconnect behavior

JDierkse há 5 anos atrás
pai
commit
2365589910
4 ficheiros alterados com 96 adições e 42 exclusões
  1. 10 0
      MySQL/MySQLClient.cpp
  2. 69 40
      MySQL/MySQLClientImpl.cpp
  3. 14 2
      MySQL/MySQLClientImpl.h
  4. 3 0
      include/MySQLClient.h

+ 10 - 0
MySQL/MySQLClient.cpp

@@ -28,6 +28,16 @@ void MySQLClient::Disconnect()
 	m_pMySQLClientImpl->Disconnect();
 }
 
+void MySQLClient::Reconnect()
+{
+	m_pMySQLClientImpl->Reconnect();
+}
+
+bool MySQLClient::Connected()
+{
+	return m_pMySQLClientImpl->Connected();
+}
+
 std::string MySQLClient::Database() const
 {
 	return m_pMySQLClientImpl->Database();

+ 69 - 40
MySQL/MySQLClientImpl.cpp

@@ -19,55 +19,40 @@ MySQLClientImpl::~MySQLClientImpl()
 void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password)
 {
 	std::unique_lock<std::mutex> lock(m_mutex);
-	try
-	{
-		if (m_pConnection)
-			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_connected = true;
-	}
-	catch (sql::SQLException &e)
-	{
-		std::stringstream ss;
-		ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
-		Logging::Log(Logging::Severity::Error, ss.str());
-	}
+	m_hostname = hostname;
+	m_username = username;
+	m_password = password;
+	m_database = "";
+	InternalConnect();
 }
 
 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);
-	try
-	{
-		if (m_pConnection)
-			Disconnect();
+	m_hostname = hostname;
+	m_username = username;
+	m_password = password;
+	m_database = database;
+	InternalConnect();
+}
 
-		std::stringstream ss;
-		ss << "tcp://" << hostname << ":3306/" << database;
+void MySQLClientImpl::Disconnect()
+{
+	std::unique_lock<std::mutex> lock(m_mutex);
+	InternalDisconnect();
+}
 
-		m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), username, password));
-		m_database = database;
-		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());
-	}
+void MySQLClientImpl::Reconnect()
+{
+	std::unique_lock<std::mutex> lock(m_mutex);
+	InternalDisconnect();
+	InternalConnect();
 }
 
-void MySQLClientImpl::Disconnect()
+bool MySQLClientImpl::Connected() const
 {
 	std::unique_lock<std::mutex> lock(m_mutex);
-	m_pConnection.reset();
-	m_database = "";
-	m_connected = false;
+	return m_connected;
 }
 
 std::string MySQLClientImpl::Database() const
@@ -77,7 +62,7 @@ std::string MySQLClientImpl::Database() const
 	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);
 	if (!m_connected)
@@ -93,11 +78,17 @@ void MySQLClientImpl::Execute(const std::string& query) const
 		std::stringstream ss;
 		ss << "MySQLClientImpl::Execute() - Error: " << e.what() << 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());
+
+		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);
 	if (!m_connected)
@@ -114,10 +105,48 @@ std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
 		std::stringstream ss;
 		ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << 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());
+
+		int errorCode = e.getErrorCode();
+		if (errorCode == 2006 || errorCode == 2013)
+			InternalDisconnect();
 	}
 
 	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

+ 14 - 2
MySQL/MySQLClientImpl.h

@@ -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, const std::string& database);
+
 	void Disconnect();
+	void Reconnect();
+	bool Connected() const;
 
 	std::string Database() const;
 
-	void Execute(const std::string& query) const;
-	std::shared_ptr<MySQLResultSetImpl> ExecuteQuery(const std::string& query) const;
+	void Execute(const std::string& query);
+	std::shared_ptr<MySQLResultSetImpl> ExecuteQuery(const std::string& query);
+
+private:
+	void InternalConnect();
+	void InternalDisconnect();
 
 private:
 	mutable std::mutex m_mutex;
+
 	bool m_connected;
+	std::string m_hostname;
+	std::string m_username;
+	std::string m_password;
 	std::string m_database;
+
 	sql::mysql::MySQL_Driver* m_pDriver;
 	std::shared_ptr<sql::Connection> m_pConnection;
 };

+ 3 - 0
include/MySQLClient.h

@@ -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, const std::string& database);
+
 	void Disconnect();
+	void Reconnect();
+	bool Connected();
 
 	std::string Database() const;