I am using nodejs built in cluster module in my application mongodb( not sharded) used for storage in each cluster(worker) connection is made using mongoose.createConnection method and closes after inserting data.
But what i am expecting is whenever a request is made it opens connection to the db and process request and close the connection.
But what i noticed that when i checking the mongodb log still open connection and its count sligtly larger than no of processor/(cluster nodes).
and i put poolSize:1,autreconect:false still some connections are not closing even after close() method is called.
my observations are when connection error happends the connection is not closed Please help me
I am using following script to get the connection .
module.exports.getDb = function () {
var dburl = 'mongodb://localhost/DB';
db = mongoose.createConnection(dburl, {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
},
auto_reconnect: false,
poolSize: 1
}
}, function (err) {
console.log(err)
});
db.on('error', function (err) {
console.log(err + " this is error");
db.close();
});
return db;
}
and i close the conneciton using db.close() in the end of evey query callback.