rahulroy9202
rahulroy9202

Reputation: 2848

how to handle connection problems in node-mongodb-native

How do I stop queries from buffering and instead throw error when connection doesn't exist between application and database?

I'm using node-mongodb-native driver.

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://someremotedb:27017/test';
var db = null,
    xcollection = null;

As suggested in this answer I am opening a connection and keeping it for later requests.

function connect() {
    MongoClient.connect(url, function(err, _db) {

        db = _db;
        xcollection = db.collection('xcollection');

        db.on('close', function() {
            console.log("connection close mongoDB");
            connection_retry();
        });

        db.on('error', function(err) {
            console.log("connection err mongoDB ",err);
            connection_retry();                
            db.close();
        });
    });
}

and I use it like this.

module.exports.xcollection_find =  function (_x, _cb) {
    try {
        xcollection.findOne({x: _x}, { _id: 0 }, function(err, doc) {
            if(err) return _cb(err, null);
            if(doc===null) return _cb(null, {success: false, data: null});
            return _cb(null, {success: true, data: doc});
        });
    }
    catch(e) {
        return _cb(e, null);
    }
}

I call connect and then everything works fine as expected.

Except for when I interrupt the internet connection to the db. (ie- disconnect internet on my pc with app running), I don't see any errors. All requests made timeouts with this message.

[MongoError: server c99.kahana.mongohq.com:27017 received an error {"name":"MongoError","message":"read ETIMEDOUT"}]

But it takes around 5 to 10 minutes before the timeout occurs.

If the close or error event is thrown my reconnect function would fire which is.

function connection_retry () {
    console.log("connection retry mongoDB");
    setTimeout(function() {
        connect();
    }, 500);
}

But it never does.
If net connection is restored before the timeouts occurs(ie- 5 -10 mins), the queries are executed and the results are received.

How do I detect that the connection is down in the xcollection_find method?

Why are the on close or on error callbacks not executed?


update:

var options = {
    db: {
        bufferMaxEntries: 2
    },
    server: {
        socketOptions: {
            keepAlive: true,
            connectTimeoutMS: 2000
        },
        auto_reconnect: true
    }
}

MongoClient.connect(url, options, function(err, _db) {

setting bufferMaxEntries to 2 still doesn't solve problem and the requests are buffered and occurs on reconnect.

Upvotes: 2

Views: 2370

Answers (1)

Abhishek Dey
Abhishek Dey

Reputation: 1639

You open do MongoClient.connect once when your app boots up and reuse the db object. It's not a singleton connection pool each .connect creates a new connection pool. So open it once an reuse across all requests.

https://groups.google.com/forum/#!msg/node-mongodb-native/mSGnnuG8C1o/Hiaqvdu1bWoJ

Node.js is single threaded you should not open as well as close the connection on same request.

Upvotes: 2

Related Questions