68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
// =====================================================
|
|
// 連線測試 API - 驗證連線釋放
|
|
// =====================================================
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { connectionMonitor } from '@/lib/connection-monitor';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// 獲取測試前的連線狀態
|
|
const beforeStats = await connectionMonitor.getConnectionStats();
|
|
|
|
// 執行一些測試查詢
|
|
const testQueries = [
|
|
'SELECT 1 as test1',
|
|
'SELECT 2 as test2',
|
|
'SELECT 3 as test3',
|
|
'SELECT COUNT(*) as user_count FROM users',
|
|
'SELECT COUNT(*) as app_count FROM apps'
|
|
];
|
|
|
|
console.log('🧪 開始連線測試...');
|
|
console.log(`測試前連線數: ${beforeStats.activeConnections}`);
|
|
|
|
// 執行測試查詢
|
|
for (let i = 0; i < testQueries.length; i++) {
|
|
const { db } = await import('@/lib/database');
|
|
await db.query(testQueries[i]);
|
|
console.log(`✅ 完成測試查詢 ${i + 1}`);
|
|
}
|
|
|
|
// 等待一小段時間讓連線釋放
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
// 獲取測試後的連線狀態
|
|
const afterStats = await connectionMonitor.getConnectionStats();
|
|
|
|
console.log(`測試後連線數: ${afterStats.activeConnections}`);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '連線測試完成',
|
|
data: {
|
|
before: {
|
|
activeConnections: beforeStats.activeConnections,
|
|
usagePercentage: beforeStats.usagePercentage
|
|
},
|
|
after: {
|
|
activeConnections: afterStats.activeConnections,
|
|
usagePercentage: afterStats.usagePercentage
|
|
},
|
|
difference: {
|
|
connectionChange: afterStats.activeConnections - beforeStats.activeConnections,
|
|
isReleased: afterStats.activeConnections <= beforeStats.activeConnections
|
|
}
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('連線測試失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '連線測試失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|