Skip to content

Commit 696b528

Browse files
committed
refactor: Refactor ConnectionPool and ServiceProvider to handle errors and reinitialization
1 parent b110916 commit 696b528

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/database/ConnectionPool.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ export class ConnectionPool {
6060
}
6161

6262
async close(): Promise<void> {
63-
if (this._isClosed) return; // Prevent closing the pool more than once
63+
if (this._isClosed) return;
6464
this._isClosed = true;
65-
await this._pool.end();
65+
try {
66+
await this._pool.end();
67+
logger.info('Connection pool closed successfully.');
68+
} catch (err: any) {
69+
logger.error('Error while closing the connection pool:', err.message);
70+
}
6671
}
6772
private initializePool(connectionString: string): Pool {
6873
return new Pool({
@@ -80,10 +85,20 @@ export class ConnectionPool {
8085
}
8186

8287
if (this._pool && !this._pool.ended) {
88+
logger.warn('Ending the current pool before reinitialization...');
8389
await this._pool.end();
8490
}
91+
8592
const newConnectionString = this.getConnectionString();
8693
this._pool = this.initializePool(newConnectionString);
87-
console.warn('Connection pool reinitialized.');
94+
95+
try {
96+
const testClient = await this._pool.connect();
97+
testClient.release(); // Ensure the new pool is functional
98+
logger.info('Connection pool reinitialized successfully.');
99+
} catch (err: any) {
100+
logger.error('Failed to reinitialize connection pool:', err.message);
101+
throw err; // Let the caller handle this failure
102+
}
88103
}
89104
}

src/service/database/ServiceProvider.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,25 @@ export class ServiceProvider {
4141
await this._connectionPool.close();
4242
}
4343
async getPoolClint(): Promise<PoolClient> {
44-
try {
45-
const client = await this._connectionPool.getClient();
46-
client.on('error', (err: any) => {
47-
logger.error('Unexpected client error:', err);
48-
});
49-
return client;
50-
} catch (err: any) {
51-
logger.error('Error getting client from pool:', err);
52-
await this._connectionPool.reinitializePool();
53-
return this.getPoolClint();
44+
let retries = 3;
45+
while (retries > 0) {
46+
try {
47+
const client = await this._connectionPool.getClient();
48+
client.on('error', (err: any) => {
49+
logger.error('Unexpected client error:', err);
50+
});
51+
return client;
52+
} catch (err: any) {
53+
logger.error('Error getting client from pool:', err.message);
54+
if (retries === 1) {
55+
logger.error('Maximum retries reached. Reinitializing the pool.');
56+
await this._connectionPool.reinitializePool();
57+
}
58+
retries -= 1;
59+
await new Promise((resolve) => setTimeout(resolve, 1000));
60+
}
5461
}
62+
throw new Error('Failed to get a database client after multiple retries.');
5563
}
5664
async getGroupService() {
5765
const client = await this.getPoolClint();

0 commit comments

Comments
 (0)