Mongoose Trying to open unclosed connection

Mongoose Trying to open unclosed connection

Problem

This is a simplified version of the problem, but basically I’m trying to open 2 mongodb connections with mongoose and it’s giving me “Trying to open unclosed connection.” error.

Code sample:

var db1 = require('mongoose');
db1.connect('my.db.ip.address', 'my-db');

var db2 = require('mongoose');
db2.connect('my.db.ip.address', 'my-db');

db2.connection.close();
db1.connection.close();

Any idea how to make it work?

Problem courtesy of: Ben Wong

Solution

connect() opens the default connection to the db. Since you want two different connections, use createConnection().

API link: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection

Solution courtesy of: Raghuveer

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *