修正資料庫未關閉問題
This commit is contained in:
70
app/api/admin/kill-connections/route.ts
Normal file
70
app/api/admin/kill-connections/route.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
// =====================================================
|
||||
// 強制終止連線 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
console.log('💀 開始強制終止所有資料庫連線...');
|
||||
|
||||
// 獲取所有 AI_Platform 的連線
|
||||
const connections = await db.query(`
|
||||
SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE
|
||||
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||||
WHERE USER = 'AI_Platform' AND DB = 'db_AI_Platform'
|
||||
`);
|
||||
|
||||
console.log(`找到 ${connections.length} 個 AI_Platform 連線`);
|
||||
|
||||
const killedConnections = [];
|
||||
|
||||
// 終止每個連線
|
||||
for (const conn of connections) {
|
||||
try {
|
||||
await db.query(`KILL CONNECTION ${conn.ID}`);
|
||||
killedConnections.push({
|
||||
id: conn.ID,
|
||||
host: conn.HOST,
|
||||
time: conn.TIME,
|
||||
command: conn.COMMAND
|
||||
});
|
||||
console.log(`✅ 已終止連線 ${conn.ID} (閒置 ${conn.TIME} 秒)`);
|
||||
} catch (error) {
|
||||
console.error(`❌ 終止連線 ${conn.ID} 失敗:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// 等待連線完全關閉
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// 檢查剩餘連線
|
||||
const remainingConnections = await db.query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||||
WHERE USER = 'AI_Platform' AND DB = 'db_AI_Platform'
|
||||
`);
|
||||
|
||||
const remainingCount = remainingConnections[0]?.count || 0;
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '強制終止連線完成',
|
||||
data: {
|
||||
totalFound: connections.length,
|
||||
killed: killedConnections.length,
|
||||
remaining: remainingCount,
|
||||
killedConnections: killedConnections
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('強制終止連線失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '強制終止連線失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user