We are using mongoose to connect to MongoDB.Initially a pool of 100 connections is created and to connect to some other database using the mongoose method useDb
Below is the code snippet
var url = require("url");
var connectRoute = require('connect-route');
var connect = require('connect'),
app = connect.createServer();
var mongoose = require('mongoose');
var conn = mongoose.createConnection('mongodb://localhost:10040/first', {server: {poolSize: 100}});
conn.on('error', console.error.bind(console, 'connection error:'));
var Schema = mongoose.Schema;
var MySchema = new Schema({
user: String,
pwd: String, roles: []
},
{strict: false}
);
app.use(connectRoute(function (router){
router.get('/get', function(req,res){
var db2 = conn.useDb('second_DB');
var data = db2.model('', MySchema, 'coll');
data.update({name: "Janu"}, {$set: {"name": "test"}}, {upsert: true} , function (err, data,log) {
//console.log(a.data.data );
res.end(JSON.stringify(log.connectionId));
});
});
}));
app.listen(3000);
console.log('info','Connect server listening on port 3000 ' );
Even though Poolsize is set to 100 , when more than 10 concurrent requests are given throwing the warning as
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
MongoDB version - 2.6.4 Mongoose version - 3.8.12
can anyone help out on why this warning am getting ??
The function conn.useDb
create a new connection object and add an event listener on it. the size of the Poolsize does not affect that.
Your code effectively have an EventEmitter memoryleak. Each times the routes is called, a new object with an event listener is created and will never be deleted.
To fix your problem : cache the return value of conn.useDb.
If you have more than 10 databases, you can increase the limit of listener at the start of the application with :
require('events').EventEmitter.defaultMaxListeners = 15;
or with
emitter.setMaxListeners()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With