mysql - Do i need to connect to database every HTTP request in NodeJS? -
i using locomotivejs(mvc) based on expressjs developing first simple api.. still in learning phase.. using mysql database..
my question is, need initiate connection mysql everytime there's controller request?
here's code :
songscontroller.show = function() { //this.title = 'locomotive'; console.log("nice imbasss"); var contacts = songsmodel.foo("good"); var dbconnection = dbutilities.connectmysql(); var contactsarr = []; dbconnection.query('select * contacts', function(err, rows, fields) { //console.log(err); console.log(rows); //console.log(fields); //contactsarr = rows; }); dbutilities.endmysql(dbconnection); };
as can notice, everytime songs/show called, connectmysql() called.. doing right?
you should connect mysql everytime when fire query mysql , should close connection after because mysql injection may occur third party.
Comments
Post a Comment