Skip to content

Commit

Permalink
BE-674 Lost connection to explorer-db
Browse files Browse the repository at this point in the history
* using connection pools instead to PostgreSQL

Signed-off-by: nfrunza <nfrunza@gmail.com>
Change-Id: Ic2944a73cf35f4d8865cf59226c4b8a6e7c70800
  • Loading branch information
nfrunza committed Oct 29, 2019
1 parent 3b68223 commit 010f25c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 109 deletions.
1 change: 0 additions & 1 deletion app/persistence/PersistenceFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class PersistenceFactory {
// Avoid to load all db Persist module
const PostgreSQL = require('./postgreSQL/Persist');
const persistence = new PostgreSQL(dbconfig);
await persistence.getPGService().handleDisconnect();
return persistence;
}
throw new ExplorerError(explorer_error.ERROR_1003, db);
Expand Down
47 changes: 15 additions & 32 deletions app/persistence/postgreSQL/PgService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
* limitations under the License.
*/

const { Client } = require('pg');
// const { Client } = require('pg');

const fs = require('fs');
const Pool = require('pg-pool');

const helper = require('../../common/helper');

Expand Down Expand Up @@ -65,7 +66,10 @@ class PgService {
* Value this.pgconfig.ssl.key is private key
*/
const { rejectUnauthorized, requestCert } = this.pgconfig.ssl;
const printConfig = { rejectUnauthorized, requestCert };
const printConfig = {
rejectUnauthorized,
requestCert
};
logger.info('SSL to Postgresql enabled with settings: ', printConfig);
} else {
logger.info('SSL to Postgresql disabled');
Expand All @@ -78,42 +82,21 @@ class PgService {

logger.info(`connecting to Postgresql ${connectionString}`);

this.client = new Client(this.pgconfig);
this.client = new Pool(this.pgconfig);

console.log('DB Pool created: ');
logger.info('DB Pool created: ');

this.client.on('error', err => {
console.log('db error:', err);
logger.error('db error:', err);
});

logger.info(
'Please set logger.setLevel to DEBUG in ./app/helper.js to log the debugging.'
);
}

/**
*
*
* @memberof PgService
*/
async handleDisconnect() {
try {
this.client.on('error', err => {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
} else {
throw err;
}
});
await this.client.connect();
} catch (err) {
if (err) {
/*
* We introduce a delay before attempting to reconnect,
* To avoid a hot loop, and to allow our node script to
* Process asynchronous requests in the meantime.
*/
console.log('error when connecting to db:', err);
setTimeout(this.handleDisconnect, 2000);
}
}
}

/**
*
*
Expand Down
106 changes: 32 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"ejs": "^2.5.6",
"enum": "^2.5.0",
"express": "^4.15.3",
"express-fileupload": "^0.4.0",
"fabric-ca-client": "^1.4.4",
"fabric-client": "^1.4.4",
"fabric-network": "^1.4.4",
Expand All @@ -41,7 +40,8 @@
"npm-run-all": "^4.1.5",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"pg": "^7.4.1",
"pg": "^7.12.1",
"pg-pool": "^2.0.7",
"prettyjson": "^1.2.1",
"prop-types": "^15.6.2",
"save": "^2.3.3",
Expand Down

0 comments on commit 010f25c

Please # to comment.