@@ -4,9 +4,6 @@ import logger from '../utils/logger';
4
4
export class ConnectionPool {
5
5
private _pool : Pool ;
6
6
private _isProduction : 'development' | 'production' ;
7
- private _isClosed : boolean = false ;
8
- private _isReinitializing : boolean = false ;
9
-
10
7
constructor ( ) {
11
8
const connectionString = this . getConnectionString ( ) ;
12
9
this . _isProduction = Config . environment ;
@@ -62,14 +59,7 @@ export class ConnectionPool {
62
59
}
63
60
64
61
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 ( ) ;
73
63
}
74
64
private initializePool ( connectionString : string ) : Pool {
75
65
return new Pool ( {
@@ -82,32 +72,11 @@ export class ConnectionPool {
82
72
} ) ;
83
73
}
84
74
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 ( ) ;
111
77
}
78
+ const newConnectionString = this . getConnectionString ( ) ;
79
+ this . _pool = this . initializePool ( newConnectionString ) ;
80
+ console . warn ( 'Connection pool reinitialized.' ) ;
112
81
}
113
82
}
0 commit comments