Node.js Mongoose在第二次请求时打开2个到DB的连接
我正在编写一个小节点,通过Mongoose进行简单的查询来监视MongoDB。如果有东西被退回,服务将被视为可用。这是我的剧本Node.js Mongoose在第二次请求时打开2个到DB的连接,node.js,mongoose,Node.js,Mongoose,我正在编写一个小节点,通过Mongoose进行简单的查询来监视MongoDB。如果有东西被退回,服务将被视为可用。这是我的剧本 var res; var logPrefix = '[MongoDB]'; var counter = 0; var mongoose = require('mongoose'); // Simple schema and model for getting a companyprofiles document var companyProfile = new mon
var res;
var logPrefix = '[MongoDB]';
var counter = 0;
var mongoose = require('mongoose');
// Simple schema and model for getting a companyprofiles document
var companyProfile = new mongoose.Schema({
_id: String
});
companyProfile.virtual('Id').get(function () {return this._id});
function closeConnection() {
console.log('8');
mongoose.connection.close(function () {
console.log('9');
console.log('%s Closed connection%d', logPrefix, counter);
});
}
function connectAndCheckHealth() {
console.log('2');
mongoose.connect('mongodb://localhost:27017/testdb');
console.log('3');
mongoose.connection.on('error', function(err) {
console.log('%s Error connecting to DB:\n%s %s', logPrefix, logPrefix, err);
res.send(503, 'ERR');
});
mongoose.connection.on('open', function() {
mongoose.connection.db.serverConfig.options.auto_reconnect = false;
});
console.log('4');
mongoose.connection.on('connected', checkHealth);
console.log('5');
mongoose.connection.on('close', function() {
console.log('%s Connection to MongoDB closed %d', logPrefix, counter);
});
}
function checkHealth() {
console.log('6');
cpModel = mongoose.model('companyProfile', companyProfile, 'companyprofiles');
cpModel.findById('spin', handleModelResponse);
}
function handleModelResponse(error, doc) {
console.log('7');
closeConnection();
console.log('10');
if (error !== null) {
console.log('11');
console.log('%s Error handling response from model:\n%s %s', logPrefix, logPrefix, error);
res.send(503, 'ERR');
} else {
console.log('12');
if (doc.Id.match(/company1/)) {
console.log('13');
console.log('%s App status is ok', logPrefix);
res.send(200, 'OK');
} else {
console.log('14');
console.log('%s Couldn\'t find the spin company profile. Found %s', logPrefix, doc.Id);
res.send(503, 'ERR');
}
}
}
module.exports = {
health: function(response) {
counter++;
var date = new Date();
console.log('%s Retrieving health from MongoDB at %s', logPrefix, date);
res = response;
console.log(mongoose.connection);
console.log('1');
connectAndCheckHealth();
console.log('15');
}
}
如您所见,我在脚本中添加了带有数字的console.log行,以尝试计算控制流。以下是输出:
1
2
3
4
5
15
6
6
6
7
8
[MongoDB] Connection to MongoDB closed 3
[MongoDB] Connection to MongoDB closed 3
[MongoDB] Connection to MongoDB closed 3
9
[MongoDB] Closed connection3
10
12
13
[MongoDB] App status is ok
7
8
9
[MongoDB] Closed connection3
10
12
13
[MongoDB] App status is ok
/home/GTP/healthcheck/node_modules/mongoose/lib/utils.js:413
throw err;
^
Error: Can't set headers after they are sent.
请注意,连接后,6在回调中出现三次。我不知道我们为什么要打开多个连接。我已关闭自动连接,并在每次请求后关闭连接
任何帮助都将不胜感激。Mongoose默认打开5个连接池;如果您只需要一个连接,您可以将连接呼叫更改为:
mongoose.connect('mongodb://localhost:27017/testdb', { server: { poolSize: 1 }});
Mongoose默认打开5个连接池;如果您只需要一个连接,您可以将连接呼叫更改为:
mongoose.connect('mongodb://localhost:27017/testdb', { server: { poolSize: 1 }});
db.once“open”对我有用
在测试我的页面时,我注意到双重和快速表单提交会触发关于已发送的标题的错误。这就停止了。db.“打开”一词对我有用
在测试我的页面时,我注意到双重和快速表单提交会触发关于已发送的标题的错误。这就停止了。我刚试过这个。它并没有阻止原帖子中描述的相同场景。@brimble2010怪异。尝试对“已连接”事件使用once而不是on:mongoose.connection.一旦“已连接”,请检查健康状况;你的回答和评论为我解决了这个问题。谢谢我刚试过这个。它并没有阻止原帖子中描述的相同场景。@brimble2010怪异。尝试对“已连接”事件使用once而不是on:mongoose.connection.一旦“已连接”,请检查健康状况;你的回答和评论为我解决了这个问题。谢谢