File tree Expand file tree Collapse file tree 2 files changed +36
-13
lines changed Expand file tree Collapse file tree 2 files changed +36
-13
lines changed Original file line number Diff line number Diff line change @@ -60,9 +60,14 @@ export class ConnectionPool {
60
60
}
61
61
62
62
async close ( ) : Promise < void > {
63
- if ( this . _isClosed ) return ; // Prevent closing the pool more than once
63
+ if ( this . _isClosed ) return ;
64
64
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
+ }
66
71
}
67
72
private initializePool ( connectionString : string ) : Pool {
68
73
return new Pool ( {
@@ -80,10 +85,20 @@ export class ConnectionPool {
80
85
}
81
86
82
87
if ( this . _pool && ! this . _pool . ended ) {
88
+ logger . warn ( 'Ending the current pool before reinitialization...' ) ;
83
89
await this . _pool . end ( ) ;
84
90
}
91
+
85
92
const newConnectionString = this . getConnectionString ( ) ;
86
93
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
+ }
88
103
}
89
104
}
Original file line number Diff line number Diff line change @@ -41,17 +41,25 @@ export class ServiceProvider {
41
41
await this . _connectionPool . close ( ) ;
42
42
}
43
43
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
+ }
54
61
}
62
+ throw new Error ( 'Failed to get a database client after multiple retries.' ) ;
55
63
}
56
64
async getGroupService ( ) {
57
65
const client = await this . getPoolClint ( ) ;
You can’t perform that action at this time.
0 commit comments