Bläddra i källkod

增加回收连接

xx 2 år sedan
förälder
incheckning
cfeef05fbd
2 ändrade filer med 23 tillägg och 9 borttagningar
  1. 20 9
      src/mysql.cpp
  2. 3 0
      src/mysql.h

+ 20 - 9
src/mysql.cpp

@@ -475,7 +475,8 @@ void module::mysql_regist(sol::state* lua)
         "update", &module::mysql::update,
         "update", &module::mysql::update,
         "insert", &module::mysql::insert,
         "insert", &module::mysql::insert,
         "delete", &module::mysql::delete_,
         "delete", &module::mysql::delete_,
-        "get", &module::mysql::get
+        "get", &module::mysql::get,
+        "recover", &module::mysql::recover
     );
     );
     module::mysql_result::regist(lua);
     module::mysql_result::regist(lua);
 
 
@@ -541,6 +542,11 @@ std::shared_ptr<module::mysql_conn> module::mysql::get()
     return std::make_shared<module::mysql_conn>(m_pool->get());
     return std::make_shared<module::mysql_conn>(m_pool->get());
 }
 }
 
 
+void module::mysql::recover(std::shared_ptr<module::mysql_conn> conn)
+{
+    conn->free();
+}
+
 void module::mysql::regist_global(const char* name, sol::state* lua)
 void module::mysql::regist_global(const char* name, sol::state* lua)
 {
 {
     lua->registry()[name] = this;
     lua->registry()[name] = this;
@@ -682,14 +688,7 @@ module::mysql_conn::mysql_conn(ylib::mysql::conn* conn)
 
 
 module::mysql_conn::~mysql_conn()
 module::mysql_conn::~mysql_conn()
 {
 {
-    if (m_conn != nullptr)
-    {
-        if (m_conn->pool() != nullptr)
-            m_conn->pool()->recover(m_conn);
-        else
-            delete m_conn;
-        m_conn = nullptr;
-    }
+    free();
 }
 }
 
 
 int module::mysql_conn::connect(const std::string& ipaddress, const std::string& username, const std::string& password, const std::string& database, const std::string& charset, ushort port)
 int module::mysql_conn::connect(const std::string& ipaddress, const std::string& username, const std::string& password, const std::string& database, const std::string& charset, ushort port)
@@ -763,6 +762,18 @@ std::shared_ptr<module::delete_> module::mysql_conn::delete_()
     return std::make_shared<module::delete_>(m_conn, false);
     return std::make_shared<module::delete_>(m_conn, false);
 }
 }
 
 
+void module::mysql_conn::free()
+{
+    if (m_conn != nullptr)
+    {
+        if (m_conn->pool() != nullptr)
+            m_conn->pool()->recover(m_conn);
+        else
+            delete m_conn;
+        m_conn = nullptr;
+    }
+}
+
 
 
 void module::mysql_conn::regist(sol::state* lua)
 void module::mysql_conn::regist(sol::state* lua)
 {
 {

+ 3 - 0
src/mysql.h

@@ -212,6 +212,7 @@ namespace module
 		std::shared_ptr<module::update> update();
 		std::shared_ptr<module::update> update();
 		std::shared_ptr<module::delete_> delete_();
 		std::shared_ptr<module::delete_> delete_();
 
 
+		void free();
 
 
 		static void regist(sol::state* lua);
 		static void regist(sol::state* lua);
 	private:
 	private:
@@ -247,6 +248,8 @@ namespace module
 		std::shared_ptr<module::delete_> delete_();
 		std::shared_ptr<module::delete_> delete_();
 
 
 		std::shared_ptr<module::mysql_conn> get();
 		std::shared_ptr<module::mysql_conn> get();
+
+		void recover(std::shared_ptr<module::mysql_conn> conn);
 	private:
 	private:
 		std::shared_ptr<ylib::mysql::pool> m_pool;
 		std::shared_ptr<ylib::mysql::pool> m_pool;