From 1dc9cd421690d628e289d8e253b1f52735bd3c63 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 5 Jan 2019 14:08:56 +0800 Subject: [PATCH] add mysql check function --- cpp/utils.cc | 14 ++++++++++++++ cpp/utils.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/cpp/utils.cc b/cpp/utils.cc index 9b2b826..5c2533b 100644 --- a/cpp/utils.cc +++ b/cpp/utils.cc @@ -120,6 +120,20 @@ namespace f8 } } + void CheckMysqlConnection(a8::mysql::Connection* conn, a8::mysql::Query* query, + std::string dbhost, int port, std::string dbuser, std::string dbpasswd, std::string gamedb) + { + if (query->ExecQuery("SELECT 1;", {}) <= 0) { + a8::UdpLog::Instance()->Warning("mysql disconnect", {}); + if (conn->Connect(dbhost, 3306, dbuser, dbpasswd, gamedb)) { + f8::InitMysqlConnection(query); + a8::UdpLog::Instance()->Info("mysql reconnect successed", {}); + } else { + a8::UdpLog::Instance()->Info("mysql reconnect failed", {}); + } + } + } + bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid) { std::vector strings; diff --git a/cpp/utils.h b/cpp/utils.h index 1fa0281..c07dc7a 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -13,6 +13,7 @@ namespace a8 { namespace mysql { + class Connection; class Query; } } @@ -78,6 +79,8 @@ namespace f8 } void InitMysqlConnection(a8::mysql::Query* query); + void CheckMysqlConnection(a8::mysql::Connection* conn, a8::mysql::Query* query, + std::string dbhost, int port, std::string dbuser, std::string dbpasswd, std::string gamedb); bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid); time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid);