Skip to content

Commit

Permalink
refactor(db): internalize pool initialization within getConnection
Browse files Browse the repository at this point in the history
getConnection now handles pool creation internally, eliminating the need for external init calls.
  • Loading branch information
JuanVillegas95 authored and LeonSilva15 committed Jul 26, 2024
1 parent fa8009e commit 7806beb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
32 changes: 17 additions & 15 deletions templates/app/utils/db/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
** All rights reserved
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/

// app/server/utils/db/config.cjs
const oracledb = require('oracledb');
const dbConfig = require('./config.cjs');

Expand All @@ -15,24 +17,24 @@ class DBConnector {
this.pool = null;
}

async init() {
try {
this.pool = await oracledb.createPool({
...dbConfig,
poolMax: 10,
poolMin: 10
});
console.log('Connection pool created successfully.');
} catch (error) {
console.error('Error creating connection pool:', error);
throw error;
async createPool() {
if (!this.pool) {
try {
this.pool = await oracledb.createPool({
...dbConfig,
poolMax: 10,
poolMin: 10
});
console.log('Connection pool created successfully.');
} catch (error) {
console.error('Error creating connection pool:', error);
throw error;
}
}
}

async getConnection(options = {}) {
if (!this.pool) {
throw new Error('Connection pool not initialized.');
}
await this.createPool();
try {
const connection = await this.pool.getConnection({
...dbConfig,
Expand All @@ -46,4 +48,4 @@ class DBConnector {
}
}

module.exports = new DBConnector();
module.exports = new DBConnector();
10 changes: 5 additions & 5 deletions templates/app/utils/rest-services/connection.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
** All rights reserved
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/
const db = require( '../db/index.cjs' );

const db = require('../db/index.cjs');

exports.getStatus = async function () {
await db.init();
const connection = await db.getConnection();
const result = await connection.execute( 'select 1 from dual' );
const result = await connection.execute('select 1 from dual');
await connection.close();

return {
status: 'ok',
}
};
};
};

0 comments on commit 7806beb

Please sign in to comment.