Skip to content

Commit

Permalink
accidentally forgot to incorporate methods to handle pool reinitializ…
Browse files Browse the repository at this point in the history
…ation and update, resolving
  • Loading branch information
siddheshraze committed Jul 9, 2024
1 parent e99b46b commit ea06968
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions frontend/components/processors/processormacros.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ const sqlConfig: PoolOptions = {
};
export const poolMonitor = new PoolMonitor(sqlConfig);

// Function to get a connection from the pool
export async function getSqlConnection(tries: number): Promise<PoolConnection> {
try {
console.log(`Attempting to get SQL connection. Try number: ${tries + 1}`);
// const connection = await pool.getConnection();

// Check if the pool is closed and reinitialize if necessary
if (poolMonitor.isPoolClosed()) {
console.log('Connection pool is closed. Reinitializing...');
poolMonitor.reinitializePool();
}

const connection = await poolMonitor.getConnection();
await connection.ping(); // Use ping to check the connection
console.log('Connection successful');
Expand Down
22 changes: 22 additions & 0 deletions frontend/config/poolmonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ export class PoolMonitor {
private totalConnectionsCreated = 0;
private waitingForConnection = 0;
private inactivityTimer: NodeJS.Timeout | null = null;
private config: PoolOptions;
private poolClosed = false;

constructor(config: PoolOptions) {
this.config = config;
this.pool = createPool(config);
this.poolClosed = false;

this.pool.on('acquire', (connection) => {
this.activeConnections++;
Expand Down Expand Up @@ -46,6 +50,10 @@ export class PoolMonitor {
}

async getConnection(): Promise<PoolConnection> {
if (this.poolClosed) {
throw new Error('Connection pool is closed');
}

try {
console.log('Requesting new connection...');
const connection = await this.pool.getConnection();
Expand All @@ -66,6 +74,7 @@ export class PoolMonitor {
try {
console.log('Ending pool connections...');
await this.pool.end();
this.poolClosed = true;
console.log('Pool connections ended.');
} catch (error) {
console.error('Error closing connections:', error);
Expand All @@ -86,4 +95,17 @@ export class PoolMonitor {
}
}, 3600000); // 1 hour in milliseconds
}

// New method to reinitialize the pool
public reinitializePool() {
console.log('Reinitializing connection pool...');
this.pool = createPool(this.config);
this.poolClosed = false;
console.log('Connection pool reinitialized.');
}

// New method to check if the pool is closed
public isPoolClosed(): boolean {
return this.poolClosed;
}
}

0 comments on commit ea06968

Please sign in to comment.