@@ -5,6 +5,8 @@ export class ConnectionPool {
5
5
private _pool : Pool ;
6
6
private _isProduction : 'development' | 'production' ;
7
7
private _isClosed : boolean = false ;
8
+ private _isReinitializing : boolean = false ;
9
+
8
10
constructor ( ) {
9
11
const connectionString = this . getConnectionString ( ) ;
10
12
this . _isProduction = Config . environment ;
@@ -83,22 +85,29 @@ export class ConnectionPool {
83
85
if ( this . _isClosed ) {
84
86
throw new Error ( 'Cannot reinitialize a closed pool.' ) ;
85
87
}
86
-
87
- if ( this . _pool && ! this . _pool . ended ) {
88
- logger . warn ( 'Ending the current pool before reinitialization...' ) ;
89
- await this . _pool . end ( ) ;
88
+ if ( this . _isReinitializing ) {
89
+ logger . warn ( 'Reinitialization already in progress. Waiting...' ) ;
90
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
91
+ return ;
90
92
}
91
93
92
- const newConnectionString = this . getConnectionString ( ) ;
93
- this . _pool = this . initializePool ( newConnectionString ) ;
94
-
94
+ this . _isReinitializing = true ;
95
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 ) ;
96
103
const testClient = await this . _pool . connect ( ) ;
97
- testClient . release ( ) ; // Ensure the new pool is functional
104
+ testClient . release ( ) ;
98
105
logger . info ( 'Connection pool reinitialized successfully.' ) ;
99
106
} catch ( err : any ) {
100
107
logger . error ( 'Failed to reinitialize connection pool:' , err . message ) ;
101
108
throw err ; // Let the caller handle this failure
109
+ } finally {
110
+ this . _isReinitializing = false ;
102
111
}
103
112
}
104
113
}
0 commit comments