node.js - Is it useful to close a MongoDB connection in NodeJS when Ctrl + C is used? -
i'm working on nodejs script using mongodb.
without going details, script take intentionally long time executed , of time finish using ctrl + c.
i write bit of code close connection when this.
but have no idea if it's useful or useless it...
here code:
mongoclient.connect(url, function (err, db) { console.log('connected correctly server'); /* magic happens here */ process.on('sigint', function () { //should ? console.log('bye'); db.close(); process.exit(); }) });
should use process.on('sigint', fct)
or leave node job?
actually, is. if connection not closed on client side, server maintains connection until timeout reached. since there limited number of connections, closing open connections should tried.
it not much of problem though, unless have a lot of connections or ram tight on server. ram might problem because server allocates stack of 1mb/connection , cursors kept until connection closed (either timeout or explicitly) or cursor exhausted.
to make long story short: yes, idea close connections made server resource efficient, not teotwawki if don't.
since signal handler not of problem, use it.
Comments
Post a Comment