I am using Observables to display real-time data in Angular2+ application and the back-end is written in NodeJs that uses MySQL database. Since I am using Observables, I need tens of millions of connections of MySQL to keep the real-time work going.
But it is not possible to acquire so many connections. So I used pooling where a connection is created from the pool of connections. However, I fail to implement it. I still get the error:
Error: ER_CON_COUNT_ERROR: Too many connections"
How can I close my connections so that the connections are not outnumbered?
Front end code:
angular.component.ts
Observable.interval(10000).subscribe(x => {
this.viewData(Val);
// more functions
console.log(" Observable")
});
NodeJS code:
dashboard.service.js
function viewData(data) {
var sqlQuery = `
select * from TRANSACTION_PAYLOAD where INTERFACE_NAME = 'Highmark' AND (STATUS ='SUCCESS_RESPONSE')`
var deferred = Q.defer();
console.log("INSIDE NODE JS SERVICE");
var host = config.host;
var user = config.user;
var password = config.password;
var database = config.database;
var con = mysql.createPool({
host: host,
user: user,
password: password,
database: database
});
con.getConnection(function (err) {
console.log("Inside .getConnection ")
if (err) deferred.reject(err.name + ': ' + err.message);
con.query(sqlQuery,
function (err, result, fields) {
if (err) deferred.reject(err.name + ': ' + err.message);
console.log(result);
deferred.resolve(result);
});
});
return deferred.promise;
con.close();
}