Enhance database connection management by introducing configurable pool settings and implementing a retry mechanism for transient connection issues. Updated Sequelize pool options to use environment variables for better flexibility and added a retry wrapper for initializing settings to handle connection timeouts gracefully.
This commit is contained in:
@@ -23,6 +23,27 @@ const queryWithTimeout = async (query, timeoutMs = 30000, description = 'Query')
|
||||
}
|
||||
};
|
||||
|
||||
// Helper: Retry wrapper for transient pool/connection issues
|
||||
const runWithRetry = async (fn, { retries = 3, delayMs = 2000, description = 'operation' } = {}) => {
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= retries; attempt++) {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
const isAcquireTimeout = error?.name === 'SequelizeConnectionAcquireTimeoutError'
|
||||
|| error?.message?.includes('ConnectionAcquireTimeoutError')
|
||||
|| error?.message?.includes('Operation timeout');
|
||||
if (!isAcquireTimeout || attempt === retries) {
|
||||
throw error;
|
||||
}
|
||||
console.warn(`⚠️ ${description} fehlgeschlagen (AcquireTimeout). Retry ${attempt}/${retries} in ${delayMs}ms...`);
|
||||
await new Promise(resolve => setTimeout(resolve, delayMs));
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
};
|
||||
|
||||
// Helper: Prüft ob Tabelle existiert
|
||||
const tableExists = async (schema, tableName) => {
|
||||
try {
|
||||
@@ -592,7 +613,10 @@ const syncDatabase = async () => {
|
||||
await syncModelsWithUpdates(models);
|
||||
|
||||
console.log("Initializing settings...");
|
||||
await initializeSettings();
|
||||
await runWithRetry(
|
||||
() => initializeSettings(),
|
||||
{ retries: 3, delayMs: 2000, description: 'initializeSettings' }
|
||||
);
|
||||
|
||||
console.log("Initializing types...");
|
||||
await initializeTypes();
|
||||
|
||||
Reference in New Issue
Block a user