优化MySQL连接池

This commit is contained in:
2024-09-06 11:57:45 +08:00
parent 672b4b771e
commit b232d6648c

View File

@@ -1,5 +1,8 @@
// MYSQL数据库连接池 /**
// 版本v0.1 * @file MySQLConnection.ts
* @version 1.0.0
* @description MySQL数据库连接池
*/
import mysql from "mysql2/promise"; import mysql from "mysql2/promise";
import Logger from "./Logger"; import Logger from "./Logger";
import config from "../config"; import config from "../config";
@@ -20,8 +23,7 @@ class MySQLConnectPool {
}, 10); }, 10);
} }
// 内部函数,无需手动调用 private createConnectPool() {
createConnectPool() {
return mysql.createPool({ return mysql.createPool({
host: config.mysql.host, host: config.mysql.host,
database: config.mysql.database, database: config.mysql.database,
@@ -33,8 +35,7 @@ class MySQLConnectPool {
}) })
} }
// 内部函数,无需手动调用 private async testConnection() {
async testConnection() {
try { try {
let res = await this.execute("SELECT 1 + 1 As result"); let res = await this.execute("SELECT 1 + 1 As result");
if (res[0].result == 2) if (res[0].result == 2)
@@ -48,8 +49,14 @@ class MySQLConnectPool {
} }
// 执行SQL语句 /**
async execute(sql: string, values?: any[], database?: string) { * 执行SQL查询
* @param sql SQL语句
* @param values 可选的查询参数列表
* @param database 可选的数据库
* @returns Promise<any | undefined> 查询结果
*/
public async execute(sql: string, values?: any[], database?: string): Promise<any | undefined> {
let connection: any; let connection: any;
try { try {
connection = await this.pool.getConnection(); connection = await this.pool.getConnection();
@@ -73,5 +80,5 @@ class MySQLConnectPool {
} }
} }
let MySQLConnection = new MySQLConnectPool(); const MySQLConnection = new MySQLConnectPool();
export default MySQLConnection; export default MySQLConnection;