Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 148089a

Browse files
JuanVillegas95LeonSilva15
authored andcommittedJul 26, 2024
fix(templates): ensure proper asynchronous initialization of DB connection pool
The DBConnector class constructor was calling an asynchronous function, leading to potential errors when the pool was not fully initialized before being used. This fix moves the asynchronous pool creation to a separate init method, ensuring the pool is ready before any connections are attempted. Closes #89
1 parent 74c51f1 commit 148089a

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed
 

‎templates/app/utils/db/index.cjs‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,38 @@ oracledb.autoCommit = true;
1212

1313
class DBConnector {
1414
constructor() {
15-
this.pool = oracledb.createPool({
16-
...dbConfig,
17-
poolMax: 10,
18-
poolMin: 10
19-
});
15+
this.pool = null;
2016
}
2117

22-
getConnection(options = {}) {
23-
const pool = oracledb.getPool();
24-
return pool.getConnection({
25-
...dbConfig,
26-
...options
27-
});
18+
async init() {
19+
try {
20+
this.pool = await oracledb.createPool({
21+
...dbConfig,
22+
poolMax: 10,
23+
poolMin: 10
24+
});
25+
console.log('Connection pool created successfully.');
26+
} catch (error) {
27+
console.error('Error creating connection pool:', error);
28+
throw error;
29+
}
30+
}
31+
32+
async getConnection(options = {}) {
33+
if (!this.pool) {
34+
throw new Error('Connection pool not initialized.');
35+
}
36+
try {
37+
const connection = await this.pool.getConnection({
38+
...dbConfig,
39+
...options
40+
});
41+
return connection;
42+
} catch (error) {
43+
console.error('Error getting connection:', error);
44+
throw error;
45+
}
2846
}
2947
}
3048

31-
module.exports = new DBConnector();
49+
module.exports = new DBConnector();

‎templates/app/utils/rest-services/connection.cjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
const db = require( '../db/index.cjs' );
88

99
exports.getStatus = async function () {
10+
await db.init();
1011
const connection = await db.getConnection();
1112
const result = await connection.execute( 'select 1 from dual' );
1213
await connection.close();
1314

1415
return {
1516
status: 'ok',
1617
}
17-
};
18+
};

0 commit comments

Comments
 (0)
Please sign in to comment.