Skip to content

Commit 010f25c

Browse files
committed
BE-674 Lost connection to explorer-db
* using connection pools instead to PostgreSQL Signed-off-by: nfrunza <nfrunza@gmail.com> Change-Id: Ic2944a73cf35f4d8865cf59226c4b8a6e7c70800
1 parent 3b68223 commit 010f25c

File tree

4 files changed

+49
-109
lines changed

4 files changed

+49
-109
lines changed

app/persistence/PersistenceFactory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class PersistenceFactory {
2525
// Avoid to load all db Persist module
2626
const PostgreSQL = require('./postgreSQL/Persist');
2727
const persistence = new PostgreSQL(dbconfig);
28-
await persistence.getPGService().handleDisconnect();
2928
return persistence;
3029
}
3130
throw new ExplorerError(explorer_error.ERROR_1003, db);

app/persistence/postgreSQL/PgService.js

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
* limitations under the License.
1919
*/
2020

21-
const { Client } = require('pg');
21+
// const { Client } = require('pg');
2222

2323
const fs = require('fs');
24+
const Pool = require('pg-pool');
2425

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

@@ -65,7 +66,10 @@ class PgService {
6566
* Value this.pgconfig.ssl.key is private key
6667
*/
6768
const { rejectUnauthorized, requestCert } = this.pgconfig.ssl;
68-
const printConfig = { rejectUnauthorized, requestCert };
69+
const printConfig = {
70+
rejectUnauthorized,
71+
requestCert
72+
};
6973
logger.info('SSL to Postgresql enabled with settings: ', printConfig);
7074
} else {
7175
logger.info('SSL to Postgresql disabled');
@@ -78,42 +82,21 @@ class PgService {
7882

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

81-
this.client = new Client(this.pgconfig);
85+
this.client = new Pool(this.pgconfig);
86+
87+
console.log('DB Pool created: ');
88+
logger.info('DB Pool created: ');
89+
90+
this.client.on('error', err => {
91+
console.log('db error:', err);
92+
logger.error('db error:', err);
93+
});
8294

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

88-
/**
89-
*
90-
*
91-
* @memberof PgService
92-
*/
93-
async handleDisconnect() {
94-
try {
95-
this.client.on('error', err => {
96-
console.log('db error', err);
97-
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
98-
this.handleDisconnect();
99-
} else {
100-
throw err;
101-
}
102-
});
103-
await this.client.connect();
104-
} catch (err) {
105-
if (err) {
106-
/*
107-
* We introduce a delay before attempting to reconnect,
108-
* To avoid a hot loop, and to allow our node script to
109-
* Process asynchronous requests in the meantime.
110-
*/
111-
console.log('error when connecting to db:', err);
112-
setTimeout(this.handleDisconnect, 2000);
113-
}
114-
}
115-
}
116-
117100
/**
118101
*
119102
*

package-lock.json

Lines changed: 32 additions & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"ejs": "^2.5.6",
2626
"enum": "^2.5.0",
2727
"express": "^4.15.3",
28-
"express-fileupload": "^0.4.0",
2928
"fabric-ca-client": "^1.4.4",
3029
"fabric-client": "^1.4.4",
3130
"fabric-network": "^1.4.4",
@@ -41,7 +40,8 @@
4140
"npm-run-all": "^4.1.5",
4241
"passport": "^0.4.0",
4342
"passport-local": "^1.0.0",
44-
"pg": "^7.4.1",
43+
"pg": "^7.12.1",
44+
"pg-pool": "^2.0.7",
4545
"prettyjson": "^1.2.1",
4646
"prop-types": "^15.6.2",
4747
"save": "^2.3.3",

0 commit comments

Comments
 (0)