Skip to content

Commit 8f91cb2

Browse files
committed
refactor: Refactor database connections and pool management
1 parent 9bdd17b commit 8f91cb2

File tree

5 files changed

+15
-25
lines changed

5 files changed

+15
-25
lines changed

src/bot/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class CopBot {
8282
app.listen(port, async () => {
8383
logger.info(`Webhook server running on port ${port}`);
8484
await this.setupWebhook(this.webhookURL);
85-
await startBot(this.mode);
8685
});
8786
} catch (err: any) {
8887
console.error('Error setting up webhook:', err);

src/database/ConnectionPool.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export class ConnectionPool {
1111
}
1212
async connect(): Promise<boolean> {
1313
try {
14-
const client = await this._pool.connect();
15-
client.release();
14+
await this._pool.connect();
1615
logger.info('Database connection successful');
1716
return true;
1817
} catch (error: any) {
@@ -23,7 +22,6 @@ export class ConnectionPool {
2322
await this.reinitializePool();
2423
try {
2524
const client = await this._pool.connect();
26-
client.release();
2725
logger.info('Database connection successful after reinitialization');
2826
return true;
2927
} catch (reconnectError: any) {
@@ -45,12 +43,8 @@ export class ConnectionPool {
4543
port,
4644
database: 'postgres',
4745
});
48-
try {
49-
await client.query(`CREATE DATABASE "${databaseName}"`);
50-
console.log(`Database "${databaseName}" created successfully.`);
51-
} finally {
52-
await client.end();
53-
}
46+
await client.query(`CREATE DATABASE "${databaseName}"`);
47+
console.log(`Database "${databaseName}" created successfully.`);
5448
}
5549
private getConnectionString(): string {
5650
const { user, host, databaseName, password, port, url } = Config.database;
@@ -73,7 +67,7 @@ export class ConnectionPool {
7367
keepAlive: true,
7468
});
7569
}
76-
private async reinitializePool() {
70+
async reinitializePool() {
7771
await this._pool.end(); // Close old connections
7872
const newConnectionString = this.getConnectionString();
7973
this._pool = this.initializePool(newConnectionString);

src/database/service/Database.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export class DatabaseService {
1818
} catch (error: any) {
1919
console.error(`Error executing query: ${sql}`, error);
2020
throw new Error(`Database query failed: ${error.message}`);
21-
} finally {
22-
this._client.release();
2321
}
2422
}
2523
/**

src/database/service/Tables.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export class TablesService {
1616
logger.info('Initial tables have been set up successfully.');
1717
} catch (error) {
1818
throw error;
19-
} finally {
20-
client.release(); // Release connection
2119
}
2220
}
2321
async seedTables() {
@@ -36,8 +34,6 @@ export class TablesService {
3634
logger.info('All tables have been seeded successfully.');
3735
} catch (error) {
3836
throw error;
39-
} finally {
40-
client.release();
4137
}
4238
}
4339
}

src/service/database/ServiceProvider.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ export class ServiceProvider {
4141
await this._connectionPool.close();
4242
}
4343
async getPoolClint(): Promise<PoolClient> {
44-
const client = await this._connectionPool.getClient();
45-
client.on('error', (err: any) => {
46-
logger.error('Unexpected client error:', err);
47-
client.release();
48-
});
49-
return client;
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();
54+
}
5055
}
5156
async getGroupService() {
5257
const client = await this.getPoolClint();
@@ -74,8 +79,6 @@ export class ServiceProvider {
7479
} catch (err: any) {
7580
logger.error('Database health check failed:', err.message);
7681
return false;
77-
} finally {
78-
client.release();
7982
}
8083
}
8184
}

0 commit comments

Comments
 (0)