修正資料庫未關閉問題

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 { db } from './database';
import { PoolConnection } from 'mysql2/promise';
export abstract class DatabaseServiceBase {
// 靜態安全查詢方法 - 確保連線關閉
// 靜態安全查詢方法 - 每次查詢完就完全關閉連線
static async safeQuery<T = any>(sql: string, params?: any[]): Promise<T[]> {
let connection: PoolConnection | null = null;
@@ -17,25 +17,27 @@ export abstract class DatabaseServiceBase {
// 執行查詢
const [rows] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線(不是釋放連線池)
await connection.destroy();
connection = null;
console.log('✅ 靜態查詢連線已完全關閉');
return rows as T[];
} catch (error) {
// 確保連線被釋放
// 確保連線被關閉
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉靜態查詢連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 實例安全查詢方法 - 確保連線關閉
// 實例安全查詢方法 - 每次查詢完就完全關閉連線
protected async safeQuery<T = any>(sql: string, params?: any[]): Promise<T[]> {
let connection: PoolConnection | null = null;
@@ -46,18 +48,20 @@ export abstract class DatabaseServiceBase {
// 執行查詢
const [rows] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線(不是釋放連線池)
await connection.destroy();
connection = null;
console.log('✅ 連線已完全關閉');
return rows as T[];
} catch (error) {
// 確保連線被釋放
// 確保連線被關閉
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉連線時發生錯誤:', destroyError);
}
}
throw error;
@@ -76,7 +80,7 @@ export abstract class DatabaseServiceBase {
return results.length > 0 ? results[0] : null;
}
// 靜態安全插入方法
// 靜態安全插入方法 - 每次查詢完就完全關閉連線
static async safeInsert(sql: string, params?: any[]): Promise<any> {
let connection: PoolConnection | null = null;
@@ -84,24 +88,26 @@ export abstract class DatabaseServiceBase {
connection = await db.getConnection();
const [result] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 靜態插入連線已完全關閉');
return result;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉靜態插入連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 實例安全插入方法
// 實例安全插入方法 - 每次查詢完就完全關閉連線
protected async safeInsert(sql: string, params?: any[]): Promise<any> {
let connection: PoolConnection | null = null;
@@ -109,24 +115,26 @@ export abstract class DatabaseServiceBase {
connection = await db.getConnection();
const [result] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 實例插入連線已完全關閉');
return result;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉實例插入連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 靜態安全更新方法
// 靜態安全更新方法 - 每次查詢完就完全關閉連線
static async safeUpdate(sql: string, params?: any[]): Promise<any> {
let connection: PoolConnection | null = null;
@@ -134,24 +142,26 @@ export abstract class DatabaseServiceBase {
connection = await db.getConnection();
const [result] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 靜態更新連線已完全關閉');
return result;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉靜態更新連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 實例安全更新方法
// 實例安全更新方法 - 每次查詢完就完全關閉連線
protected async safeUpdate(sql: string, params?: any[]): Promise<any> {
let connection: PoolConnection | null = null;
@@ -159,24 +169,26 @@ export abstract class DatabaseServiceBase {
connection = await db.getConnection();
const [result] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 實例更新連線已完全關閉');
return result;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉實例更新連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 靜態安全刪除方法
// 靜態安全刪除方法 - 每次查詢完就完全關閉連線
static async safeDelete(sql: string, params?: any[]): Promise<any> {
let connection: PoolConnection | null = null;
@@ -184,24 +196,26 @@ export abstract class DatabaseServiceBase {
connection = await db.getConnection();
const [result] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 靜態刪除連線已完全關閉');
return result;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉靜態刪除連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 實例安全刪除方法
// 實例安全刪除方法 - 每次查詢完就完全關閉連線
protected async safeDelete(sql: string, params?: any[]): Promise<any> {
let connection: PoolConnection | null = null;
@@ -209,24 +223,26 @@ export abstract class DatabaseServiceBase {
connection = await db.getConnection();
const [result] = await connection.execute(sql, params);
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 實例刪除連線已完全關閉');
return result;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉實例刪除連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 事務處理方法
// 事務處理方法 - 每次查詢完就完全關閉連線
protected async withTransaction<T>(
callback: (connection: PoolConnection) => Promise<T>
): Promise<T> {
@@ -240,10 +256,12 @@ export abstract class DatabaseServiceBase {
await connection.commit();
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 事務連線已完全關閉');
return result;
} catch (error) {
if (connection) {
@@ -254,16 +272,16 @@ export abstract class DatabaseServiceBase {
}
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉事務連線時發生錯誤:', destroyError);
}
}
throw error;
}
}
// 批次查詢方法 - 減少連線使用
// 批次查詢方法 - 每次查詢完就完全關閉連線
protected async batchQuery<T = any>(
queries: Array<{ sql: string; params?: any[] }>
): Promise<T[][]> {
@@ -278,17 +296,19 @@ export abstract class DatabaseServiceBase {
results.push(rows as T[]);
}
// 立即釋放連線
connection.release();
// 完全關閉連線
await connection.destroy();
connection = null;
console.log('✅ 批次查詢連線已完全關閉');
return results;
} catch (error) {
if (connection) {
try {
connection.release();
} catch (releaseError) {
console.error('釋放連線時發生錯誤:', releaseError);
await connection.destroy();
} catch (destroyError) {
console.error('關閉批次查詢連線時發生錯誤:', destroyError);
}
}
throw error;