javascript - node Cron with callback getting Cannot enqueue Handshake after invoking quit error -
i trying query db in particular interval. using node corn. creating connection every minute , close once query completed. works first minute, next minute throws error of cannot enqueue handshake after invoking quit, using callback. 1 correct doing wrong
app.js
var scontroller = require('./scontroller'); cron.schedule('* * * * *',function(){ console.log("i running every minute"); var params ="dddd"; scontroller.updatedb(params,function(err,res){ console.log("xxxx"); }); });
scontroller.js
var smodel = require('./smodel'); var scontroller = function(){} scontroller.prototype.updatedb = function(params,callback){ smodel.updatedb(params,function(res,err){ if(res){ console.log("kkk"); } callback(err,res); }); } module.exports = new scontroller();
smodel.js
var connection = mysql.createconnection(mysqlconfig); function mysqlconnect() { connection.connect(); } function mysqlclose() { if(!connection._protocol._ended){ connection.end(); } } var smodel = function() {}; smodel.prototype.updatedb = function(params,callback){ updatedb(params, function(err, res) { callback(err,res); }); }; function updatedb(params,callback){ var query = 'select * tablename'; mysqlconnect(); connection.query(query,function(err,rows,fields){ if(!err){ console.log(rows.length); } callback(err,rows); }); mysqlclose(); } module.exports = new smodel();
short answer
you should use mysql connections pool. offical doc https://github.com/mysqljs/mysql#pooling-connections
long answer
server take enough time & resource create connection. pool save connection instance , reuse. see info https://en.wikipedia.org/wiki/connection_pool
wiki
Comments
Post a Comment