Skip to content

Commit e7dba97

Browse files
committed
reafactor: remove unused properties and simplify close and reinitialize methods`
1 parent 085d968 commit e7dba97

File tree

1 file changed

+6
-37
lines changed

1 file changed

+6
-37
lines changed

src/database/ConnectionPool.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import logger from '../utils/logger';
44
export class ConnectionPool {
55
private _pool: Pool;
66
private _isProduction: 'development' | 'production';
7-
private _isClosed: boolean = false;
8-
private _isReinitializing: boolean = false;
9-
107
constructor() {
118
const connectionString = this.getConnectionString();
129
this._isProduction = Config.environment;
@@ -62,14 +59,7 @@ export class ConnectionPool {
6259
}
6360

6461
async close(): Promise<void> {
65-
if (this._isClosed) return;
66-
this._isClosed = true;
67-
try {
68-
await this._pool.end();
69-
logger.info('Connection pool closed successfully.');
70-
} catch (err: any) {
71-
logger.error('Error while closing the connection pool:', err.message);
72-
}
62+
await this._pool.end();
7363
}
7464
private initializePool(connectionString: string): Pool {
7565
return new Pool({
@@ -82,32 +72,11 @@ export class ConnectionPool {
8272
});
8373
}
8474
async reinitializePool() {
85-
if (this._isClosed) {
86-
throw new Error('Cannot reinitialize a closed pool.');
87-
}
88-
if (this._isReinitializing) {
89-
logger.warn('Reinitialization already in progress. Waiting...');
90-
await new Promise((resolve) => setTimeout(resolve, 2000));
91-
return;
92-
}
93-
94-
this._isReinitializing = true;
95-
try {
96-
if (this._pool && !this._pool.ended) {
97-
logger.warn('Ending the current pool before reinitialization...');
98-
await this._pool.end();
99-
}
100-
101-
const newConnectionString = this.getConnectionString();
102-
this._pool = this.initializePool(newConnectionString);
103-
const testClient = await this._pool.connect();
104-
testClient.release();
105-
logger.info('Connection pool reinitialized successfully.');
106-
} catch (err: any) {
107-
logger.error('Failed to reinitialize connection pool:', err.message);
108-
throw err; // Let the caller handle this failure
109-
} finally {
110-
this._isReinitializing = false;
75+
if (this._pool && !this._pool.ended) {
76+
await this._pool.end();
11177
}
78+
const newConnectionString = this.getConnectionString();
79+
this._pool = this.initializePool(newConnectionString);
80+
console.warn('Connection pool reinitialized.');
11281
}
11382
}

0 commit comments

Comments
 (0)