title: how to handle connection problems in node-mongodb-native
body: <p><strong>How do I stop queries from buffering and instead throw error when connection doesn't exist between application and database?</strong></p>
  
  <p>I'm using <a href="https://github.com/mongodb/node-mongodb-native" rel="nofollow noreferrer">node-mongodb-native</a> driver.</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient;
  var url = 'mongodb://someremotedb:27017/test';
  var db = null,
      xcollection = null;
  </code></pre>
  
  <p>As <a href="https://stackoverflow.com/a/14464750/2232902">suggested in this answer</a> I am opening a connection and keeping it for later requests.</p>
  
  <pre><code>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();
          });
      });
  }
  </code></pre>
  
  <p>and I use it like this.</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>I call connect and then everything works fine as expected.</p>
  
  <p>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.</p>
  
  <pre><code>[MongoError: server c99.kahana.mongohq.com:27017 received an error {"name":"MongoError","message":"read ETIMEDOUT"}]
  </code></pre>
  
  <p>But it takes around 5 to 10 minutes before the timeout occurs.</p>
  
  <p>If the close or error event is thrown my reconnect function would fire which is.</p>
  
  <pre><code>function connection_retry () {
      console.log("connection retry mongoDB");
      setTimeout(function() {
          connect();
      }, 500);
  }
  </code></pre>
  
  <p>But it never does.<br>
  If net connection is restored before the timeouts occurs(ie- 5 -10 mins), the queries are executed and the results are received.</p>
  
  <p><strong>How do I detect that the connection is down in the xcollection_find method?</strong></p>
  
  <p><strong>Why are the on close or on error callbacks not executed?</strong></p>
  
  <hr>
  
  <p>update:</p>
  
  <pre><code>var options = {
      db: {
          bufferMaxEntries: 2
      },
      server: {
          socketOptions: {
              keepAlive: true,
              connectTimeoutMS: 2000
          },
          auto_reconnect: true
      }
  }
  
  MongoClient.connect(url, options, function(err, _db) {
  </code></pre>
  
  <p>setting bufferMaxEntries to 2 still doesn't solve problem and the requests are buffered and occurs on reconnect.</p>