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.
这是问题的简化版本,但是基本上我正在尝试与mongoose打开两个mongodb连接,它给了我“尝试打开未关闭连接”的错误。
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 个解决方案
#1
44
connect()
opens the default connection to the db. Since you want two different connections, use createConnection()
.
connect()打开到db的默认连接。因为您需要两个不同的连接,所以使用createConnection()。
API link: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection
API链接:http://mongoosejs.com/docs/api.html index_Mongoose-createConnection
#2
2
To add on Raghuveer answer :
补充一下Raghuveer的回答:
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(...);
#3
2
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
}
#4
2
I had this problem doing unit test with mocha
.
我在用摩卡做单元测试时遇到了这个问题。
The problem came when I added a second test because beforeEach
is called twice.
当我添加第二个测试时,出现了问题,因为beforeEach被调用两次。
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
0
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');
#6
0
Using mongoose.disconnect(fn):
使用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.
我发现这个问题输入错误信息,尽管我的问题有点不同,我相信它可能对那些使用Hapi的人有用。更具体地说,Hapi + res - Hapi + mocha。
When running mocha
with --watch
option I was facing both: OverwriteModelError
and Error: Trying to open unclosed connection errors
.
当使用-watch运行mocha时,我同时面临两个问题:OverwriteModelError和Error:试图打开未关闭的连接错误。
#1
44
connect()
opens the default connection to the db. Since you want two different connections, use createConnection()
.
connect()打开到db的默认连接。因为您需要两个不同的连接,所以使用createConnection()。
API link: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection
API链接:http://mongoosejs.com/docs/api.html index_Mongoose-createConnection
#2
2
To add on Raghuveer answer :
补充一下Raghuveer的回答:
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(...);
#3
2
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
}
#4
2
I had this problem doing unit test with mocha
.
我在用摩卡做单元测试时遇到了这个问题。
The problem came when I added a second test because beforeEach
is called twice.
当我添加第二个测试时,出现了问题,因为beforeEach被调用两次。
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
0
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');
#6
0
Using mongoose.disconnect(fn):
使用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.
我发现这个问题输入错误信息,尽管我的问题有点不同,我相信它可能对那些使用Hapi的人有用。更具体地说,Hapi + res - Hapi + mocha。
When running mocha
with --watch
option I was facing both: OverwriteModelError
and Error: Trying to open unclosed connection errors
.
当使用-watch运行mocha时,我同时面临两个问题:OverwriteModelError和Error:试图打开未关闭的连接错误。