diff --git a/README.md b/README.md index 5abf043..d109acb 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,9 @@ If you wish to use NVM to maintain parallel Node installations, ensure that it d ## References - https://oracle.github.io/node-oracledb/INSTALL.html#quickstart + +### Changelog + +# v1.0.2 +Added auto drop and create pool when getting connection timedout +to fix NJS-040 issue. diff --git a/index.js b/index.js index e3fb9a4..40bf638 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,13 @@ exports.createPool = async (poolName) => { } }; -exports.connect = async (poolName) => { +/** + * + * @param {string} poolName Connection pool name/alias + * @param {number} retryCount Amount of retries. Default = 2 + * @returns {oracledb.Connection} + */ +exports.connect = async (poolName, retryCount = 2) => { try { if (!pools[poolName]) { await this.createPool(poolName); @@ -74,6 +80,15 @@ exports.connect = async (poolName) => { console.debug(`Oracle Adapter: Successfully retrieved a connection from ${poolName} pool`); return connection; } catch (err) { + if (JSON.stringify(err).includes('NJS-040') && retryCount >= 0) { + console.error(`Oracle Adapter: Getting pool(${poolAlias}) connection timedout.`); + promises = {}; + pools = {}; + console.debug(`Oracle Adapter: Closing pool ${poolAlias}.`); + await oracledb.getPool(poolName).close(0); + console.debug(`Oracle Adapter: Retrying to connect to ${poolAlias}.`); + return this.connect(poolName, retryCount - 1); + } console.error("Oracle Adapter: Error while retrieving a connection", err); throw new Error(err.message); } diff --git a/package.json b/package.json index ded43ef..57aad11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@softrams/nodejs-oracle-connector", - "version": "1.0.1", + "version": "1.0.2", "description": "Database connector wrapper to work with Oracle database from nodejs applications", "main": "index.js", "scripts": {