Browse Source

🐞 fix: 修复数据库连接管理和自动重连逻辑

Pchen. 7 months ago
parent
commit
861a7d82d2
2 changed files with 25 additions and 21 deletions
  1. 25 17
      plugin/DataBase/MySQL.js
  2. 0 4
      plugin/DataBase/db.js

+ 25 - 17
plugin/DataBase/MySQL.js

@@ -1,5 +1,5 @@
 const mysql = require('mysql2/promise');
 const mysql = require('mysql2/promise');
-const path = require('path')
+const path = require('path');
 const config = require('../../config.json');
 const config = require('../../config.json');
 const Logger = require('../../lib/Logger');
 const Logger = require('../../lib/Logger');
 
 
@@ -7,28 +7,37 @@ class MySQL {
     constructor() {
     constructor() {
         this.config = config.database;
         this.config = config.database;
         this.connection = null;
         this.connection = null;
-
-        this.logger = new Logger(path.join(__dirname, '../../logs/MySQL.log'), 'INFO')
+        this.logger = new Logger(path.join(__dirname, '../../logs/MySQL.log'), 'INFO');
     }
     }
 
 
     async connect() {
     async connect() {
-        if (this.connection) {
-            return this.connection;
-        }
-
-        try {
-            this.logger.info('正在连接数据库')
-            this.connection = await mysql.createConnection(this.config);
-            this.logger.info('已连接到数据库')
-            return this.connection;
-        } catch (error) {
-            this.logger.error('连接数据库失败:', error.message)
-            throw error;
+        if (!this.connection) {
+            try {
+                this.logger.info('正在连接数据库');
+                this.connection = await mysql.createConnection(this.config);
+                this.logger.info('已连接到数据库');
+                return this.connection;
+            } catch (error) {
+                this.logger.error('连接数据库失败:', error.message);
+                throw error;
+            }
+        } else {
+            try {
+                // 检查当前连接的有效性
+                await this.connection.ping();
+                return this.connection;
+            } catch (error) {
+                this.logger.warn('数据库连接失效,尝试重新连接...');
+                this.connection = null; // 清除当前连接
+                return await this.connect(); // 递归调用 connect() 重新连接
+            }
         }
         }
     }
     }
 
 
     async query(sql, params = []) {
     async query(sql, params = []) {
         try {
         try {
+            // 确保在执行查询之前连接已建立并有效
+            await this.connect();
             const [rows] = await this.connection.execute(sql, params);
             const [rows] = await this.connection.execute(sql, params);
             return rows;
             return rows;
         } catch (error) {
         } catch (error) {
@@ -40,7 +49,7 @@ class MySQL {
         if (this.connection) {
         if (this.connection) {
             try {
             try {
                 await this.connection.end();
                 await this.connection.end();
-                this.logger.info('已关闭与数据库的连接')
+                this.logger.info('已关闭与数据库的连接');
                 this.connection = null;
                 this.connection = null;
             } catch (error) {
             } catch (error) {
                 this.logger.error('关闭与数据库的连接时出错:', error);
                 this.logger.error('关闭与数据库的连接时出错:', error);
@@ -48,7 +57,6 @@ class MySQL {
             }
             }
         }
         }
     }
     }
-
 }
 }
 
 
 module.exports = MySQL;
 module.exports = MySQL;

+ 0 - 4
plugin/DataBase/db.js

@@ -2,8 +2,4 @@ const MySQL = require('./MySQL');
 
 
 const db = new MySQL();
 const db = new MySQL();
 
 
-(async () => {
-    await db.connect();
-})();
-
 module.exports = db;
 module.exports = db;