I am using Heroku Postgres and every time I run a query in my application the connection remains open. This leads to too many simultaneous connections and hence errors.
When in deployment, after I use my application for a bit I get this error:
{"name":"error","length":109,"severity":"FATAL","code":"53300","file":"miscinit.c","line":"551","routine":"InitializeSessionUserId"}
When I am running locally I get this error:
{"name":"error","length":109,"severity":"FATAL","code":"53300","file":"miscinit.c","line":"551","routine":"InitializeSessionUserId"}
This is the way I am handle queries every time I need to query the database:
router.route('/campaigns')
.get(restrictTo('advertiser'), function(req, res) {
pg.defaults.ssl = true;
pg.connect(`DATABASE_URL`, function(err, client, done) {
if (err) {
console.log('Connection issue when retrieving data, error will be thrown: ' + JSON.stringify(err));
throw err;
} else {
client.query(`SELECT * FROM campaign WHERE agentid = ${req.user.agentid};`,
function (err, result) {
if(err)
console.log(err.toString());
res.render('console/campaigns', result);
});
}
})
})
I am doing something wrong because the connections are not closing I believe. How can I solve it?