修正資料庫未關閉問題

This commit is contained in:
2025-09-21 21:23:47 +08:00
parent 36e29c5a3f
commit 38ae30d611
11 changed files with 684 additions and 111 deletions

View File

@@ -6,7 +6,7 @@ import mysql from 'mysql2/promise';
import { dbFailover } from './database-failover';
import { dbSync } from './database-sync';
// 資料庫配置
// 資料庫配置 - 強制快速釋放連線
const dbConfig = {
host: process.env.DB_HOST || '122.100.99.161',
port: parseInt(process.env.DB_PORT || '43306'),
@@ -16,19 +16,22 @@ const dbConfig = {
charset: 'utf8mb4',
timezone: '+08:00',
acquireTimeout: 5000, // 5秒獲取連接超時
timeout: 8000, // 8秒查詢超時
timeout: 10000, // 10秒查詢超時
reconnect: true,
connectionLimit: 3, // 進一步減少連接數,避免 Too many connections
queueLimit: 5, // 減少排隊數量
connectionLimit: 5, // 大幅減少連接數限制
queueLimit: 10, // 減少排隊數量
// 添加連接重試和錯誤處理配置
retryDelay: 1000,
maxRetries: 2,
// 添加連接池配置
idleTimeout: 30000, // 30秒空閒超時,快速釋放連接
maxIdle: 2, // 最空閒連接
// 強制快速釋放連線的配置
idleTimeout: 5000, // 5秒空閒超時,強制快速釋放
maxIdle: 1, // 最多只保留1個空閒連接
// 添加連接生命週期管理
maxReconnects: 3,
reconnectDelay: 2000,
reconnectDelay: 1000,
// 強制關閉空閒連線
keepAliveInitialDelay: 0,
enableKeepAlive: false, // 關閉 keepAlive
// 添加 SSL 配置(如果需要)
ssl: false as any,
};