Mongoose在第二次请求时打开2个与DB的连接

时间:2012-11-06 14:51:48

标签: 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 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出现三次(我们连接后的回调)。我不知道为什么我们要打开多个连接。我已经关闭了autoconnect并在每次请求后关闭了连接。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

Mongoose默认打开一个包含5个连接的池;如果您只想要一次连接,则可以将connect来电更改为:

mongoose.connect('mongodb://localhost:27017/testdb', { server: { poolSize: 1 }});

答案 1 :(得分:1)

db.once('open')为我工作。

在测试我的页面时,我注意到双重和快速表单提交触发了已发送标头的错误。这阻止了。