最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoDB: >5 open connections for a single db handler - Stack Overflow

programmeradmin8浏览0评论

I'm new to nodejs and mongdb, which I'll use in a project of mine, and as soon as I got the db connection working I was shocked to see how many database connections my code really does. So, given this simple snippet code:

var express = require('express');
var mongo = require('mongodb');  
var app = express();

// Further details:
// nodejs: v0.8.18
// mongod: v2.2.2
// node's mongodb driver: v1.2.10

app.get('/', function(req, res){
    res.send('<h1>Ok</h1>');
});

var setUp = function() {   
    // get a handler to the testDB Database
    mongo.Db.connect('mongodb://localhost:27017/testDB', function(err, db) {
        if (err)
            throw err;
        // create a test collection in the database
        db.createCollection('test', function(err, test) {
            if (err)
                throw err;
            // insert a dummy document into the test collection          
            test.insert({'name':'admin', 'pass':'admin'});

            app.listen(3000); 
            console.log('App listening on port 3000');
        });
    });
}

setUp();

the mongo daemon outputs this bit of log when the nodejs process fires up:

... connection accepted from 127.0.0.1:40963 #34 (1 connection now open)
... connection accepted from 127.0.0.1:40964 #35 (2 connections now open)
... connection accepted from 127.0.0.1:40965 #36 (3 connections now open)
... connection accepted from 127.0.0.1:40966 #37 (4 connections now open)
... connection accepted from 127.0.0.1:40967 #38 (5 connections now open)
... connection accepted from 127.0.0.1:40968 #39 (6 connections now open)
... end connection 127.0.0.1:40963 (5 connections now open)
... allocating new datafile /var/data/testDB.ns, filling with zeroes...
...

and this one when the process is terminated:

... connection 127.0.0.1:40964 (4 connections now open)
... connection 127.0.0.1:40965 (3 connections now open)
... connection 127.0.0.1:40966 (2 connections now open)
... connection 127.0.0.1:40967 (1 connection now open)
... connection 127.0.0.1:40968 (0 connections now open)

Does the mongo driver really need to make that many connections to mongod in order to get a single db handler, or is there something wrong with the way I implemented this? I really expected to see just one open connection in there...

I'm new to nodejs and mongdb, which I'll use in a project of mine, and as soon as I got the db connection working I was shocked to see how many database connections my code really does. So, given this simple snippet code:

var express = require('express');
var mongo = require('mongodb');  
var app = express();

// Further details:
// nodejs: v0.8.18
// mongod: v2.2.2
// node's mongodb driver: v1.2.10

app.get('/', function(req, res){
    res.send('<h1>Ok</h1>');
});

var setUp = function() {   
    // get a handler to the testDB Database
    mongo.Db.connect('mongodb://localhost:27017/testDB', function(err, db) {
        if (err)
            throw err;
        // create a test collection in the database
        db.createCollection('test', function(err, test) {
            if (err)
                throw err;
            // insert a dummy document into the test collection          
            test.insert({'name':'admin', 'pass':'admin'});

            app.listen(3000); 
            console.log('App listening on port 3000');
        });
    });
}

setUp();

the mongo daemon outputs this bit of log when the nodejs process fires up:

... connection accepted from 127.0.0.1:40963 #34 (1 connection now open)
... connection accepted from 127.0.0.1:40964 #35 (2 connections now open)
... connection accepted from 127.0.0.1:40965 #36 (3 connections now open)
... connection accepted from 127.0.0.1:40966 #37 (4 connections now open)
... connection accepted from 127.0.0.1:40967 #38 (5 connections now open)
... connection accepted from 127.0.0.1:40968 #39 (6 connections now open)
... end connection 127.0.0.1:40963 (5 connections now open)
... allocating new datafile /var/data/testDB.ns, filling with zeroes...
...

and this one when the process is terminated:

... connection 127.0.0.1:40964 (4 connections now open)
... connection 127.0.0.1:40965 (3 connections now open)
... connection 127.0.0.1:40966 (2 connections now open)
... connection 127.0.0.1:40967 (1 connection now open)
... connection 127.0.0.1:40968 (0 connections now open)

Does the mongo driver really need to make that many connections to mongod in order to get a single db handler, or is there something wrong with the way I implemented this? I really expected to see just one open connection in there...

Share Improve this question asked Jan 29, 2013 at 23:12 Marcel HernandezMarcel Hernandez 1,39113 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 20

Db.connect opens a pool of 5 connections, by default. If you want to limit it to a single connection you can do that via the server options like this:

mongo.Db.connect(
    'mongodb://localhost:27017/testDB', 
    {server: {poolSize: 1}}, 
    function(err, db) { ...

You can also pass it like that

var mongoclient = new mongodb.MongoClient(new mongodb.Server(
    'localhost', 27017, {'native_parser': true, 'poolSize': 1} 
));
发布评论

评论列表(0)

  1. 暂无评论