Maxime
Maxime

Reputation: 2260

Multiple connections with node-mongodb-native


I am working on a function to insert a document in a mongoDb database using the node-mongodb-native module. Everything is working, except if I call insert multiple documents back-to-back. I use a for loop to test how my function is reacting to multiple document insert at the same time.

var server = new Server("xxx.xxx.xxx.xxx", 27017, {auto_reconnect: true, poolSize: 100});
var db = new Db("testDb", server, {safe: false}); 

module.exports.insert = function(document){
    var database;

    function db_open(err, db_local){
        if(err) throw err;
        database = db_local;
        database.collection("rooms", handle_insert);
    }
    function handle_insert(err, collection){
        if(err) throw err;
        collection.insert(document);
        database.close();
    }
    db.open(db_open);
};

for(var i=0; i<100; i++){
    module.exports.insert({name : "test"});
}

When I'm running this code I get the error db object already connecting, open cannot be called multiple times To resolve the problem, I decided to create a new instance of Server and Db at each call of the function :

module.exports.insert = function(document){
    var database;
    var server = new Server("xxx.xxx.xxx.xxx", 27017, {auto_reconnect: true, poolSize: 100});
    var db = new Db("testDb", server, {safe: false}); 

    function db_open(err, db_local){
        if(err) throw err;
        database = db_local;
        database.collection("rooms", handle_insert);
    }
    function handle_insert(err, collection){
        if(err) throw err;
        collection.insert(document);
        database.close();
    }
    db.open(db_open);
};

for(var i=0; i<100; i++){
    module.exports.insert({name : "test"});
}

But now I'm getting connection closed thrown by the db_open function I really don't understand why my connection is closing between the moment when I'm creating db and when my code call db_open.
Have you an idea of what is happening?

Thank you :)

(Sorry if my English is not really good)

EDIT I found this website explaining that the problem is caused by the too long tcp_keepalive time. The problem with this solution is my workstation (Cloud 9). I don't have the permission to access the file /proc/sys/net/ipv4/tcp_keepalive_time

Upvotes: 0

Views: 545

Answers (1)

Bret Copeland
Bret Copeland

Reputation: 24080

I don't think your problem has anything to do with TCP keep-alive. Like the error message says, you're simply attempting to open the same connection multiple times (every time the insert method is called). Instead of calling open() every time, just call it once, and make sure the callback returns, before you call insert() for the first time. There is no hard limit to the number of simultaneous inserts you can perform on the same connection since it's all asynchronous.

Upvotes: 1

Related Questions