Mongoose Trying to open unclosed connection

2020-02-17 06:28发布

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?

6条回答
贼婆χ
2楼-- · 2020-02-17 06:53

Using mongoose.disconnect(fn):

mongoose.disconnect(() => {

  // here it would be possible "reset" models to fix 
  // OverwriteModelError errors
  mongoose.models = {};

  // here comes your logic like registering Hapi plugins
  server.register(somePlugin, callback);
});

I found this question typing the error message and despite my problem is a bit different I believe it could be useful for those using Hapi. More specifically Hapi + rest-hapi + mocha.

When running mocha with --watch option I was facing both: OverwriteModelError and Error: Trying to open unclosed connection errors.

查看更多
家丑人穷心不美
3楼-- · 2020-02-17 07:01

To add on Raghuveer answer :

I would also mention that instead of using mongoose directly (you are probably using it this way you end up on this post) :

require('mongoose').model(...);

You would use the returned connection :

var db = require('mongoose').connect('xxx', 'yyy');
db.model(...);
查看更多
做个烂人
4楼-- · 2020-02-17 07:06

I had this problem doing unit test with mocha.

The problem came when I added a second test because beforeEach is called twice.

I've solved this with this code:

const mongoose = require('mongoose');
describe('Your test suite', () => {
    beforeEach( () => {
        if (mongoose.connection.db) {
            return; // or done();
        } else {
            // connect to mongodb
    });

    describe('GET /some-path', () => {
       it('It should...', () => {

       });
    });

    describe('POST /some-path', () => {
       it('It should...', () => {

       });
    });
});

Hope it helps you!

查看更多
再贱就再见
5楼-- · 2020-02-17 07:08

You are attempting to open the default connection ( which is not yet closed ) a 2nd time.

do the following instead

var db = require('mongoose'); //note only one 'require' needed.
var connectionToDb1 = db.createConnection('my.db1.ip.address', 'my-db1');
var connectionToDb2 = db.createConnection('my.db2.ip.address', 'my-db2');
查看更多
Lonely孤独者°
6楼-- · 2020-02-17 07:10

I get this issue while running my tests.

This is what I did to solve it.

//- in my app.js file.
try {
    mongoose.connect('mongodb://localhost/userApi2'); //- starting a db connection
}catch(err) {
    mongoose.createConnection('mongodb://localhost/userApi2'); //- starting another db connection
}

查看更多
手持菜刀,她持情操
7楼-- · 2020-02-17 07:13

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

查看更多
登录 后发表回答