mongojs icon indicating copy to clipboard operation
mongojs copied to clipboard

Reconnect not working when using find

Open jasonmcaffee opened this issue 9 years ago • 7 comments

If you bring the db down for over 30 seconds, mongojs does not recover or reconnect. Db.on('error') is never fired.

Test code:

'use strict';
let mongojs = require('mongojs');
let db = mongojs('localhost:27017/testCollection', ['test'], {
  auto_reconnect: true
});
db.on('error', (e)=>{
  console.error('db error %s', e);
});
let testCollection = db.test;
setInterval(()=>{
  console.log('fetching...');
  testCollection.find(null, (err, result)=>{
    if(err){
      console.error('error fetching %s', err);
    }else{
      console.log('fetch result came back.');
    }
  });
}, 2000);

Result:

fetching...
fetch result came back.
fetching...
fetch result came back.
fetching...
fetch result came back.
fetching...
fetch result came back.
fetching...    <-- bring db down
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
error fetching MongoError: no connection available for operation    <-- takes about 32 seconds to get this error
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
error fetching MongoError: no connection available for operation
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...         <-- bring db back up
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...
fetching...

Node version: 4.0.0 Mongojs version: 2.3.0 Mongo version: 2.6

jasonmcaffee avatar Mar 09 '16 17:03 jasonmcaffee

We're using mongodb native driver under the hood so connection options get passed to mongodb native. To pass a auto reconnect option see http://mongodb.github.io/node-mongodb-native/2.1/reference/connecting/connection-settings/

The fun thing about this bug is, that autoReconnect should be enabled by default.

saintedlama avatar Mar 10 '16 08:03 saintedlama

With this approach, an error is never fired, and all finds execute when the db comes back up, regardless of how long it's down (i tested for 3 minute outage)

'use strict';

var Db = require('mongodb').Db,
  Mongos = require('mongodb').Mongos,
  Server = require('mongodb').Server;
// Connect using Mongos
var server = new Server('localhost', 27017, {auto_reconnect:true});
var db = new Db('testCollection', new Mongos([server]));
db.open(function(err, db) {
  setInterval(()=>{
    console.log('fetching...');
    db.collection('test').find({}).toArray((err, result)=>{
      if(err){
        console.error('error fetching: %s', err);
      }else{
        console.log('fetch successful' + JSON.stringify(result[0]));
      }
    });

  }, 2000);
});

jasonmcaffee avatar Mar 10 '16 19:03 jasonmcaffee

hi, I'am having a similar is, but with mongojs version 1.4.1. is it a known issue? would upgrading to latest version solve this? would 2.3.0 be backwards compatible with 1.4.1? (I suppose it will)

yonatan-py avatar Mar 22 '16 13:03 yonatan-py

I'm also seeing this behavior for mongojs going down for over 30 seconds. It times out, and never recovers. Requires a restart of the node.js process to re-connect.

geekbleek avatar Aug 14 '16 07:08 geekbleek

Hi, I have the similar error it's necessary restart node app for reconnect with db.

jsricarde avatar Jan 17 '17 20:01 jsricarde

I also face this problem. Any suggestion on this ?

dsin avatar Apr 02 '17 14:04 dsin

This issue maybe caused by the default values of reconnectTries (30 times) and reconnectInterval (1 second) based on the doc http://mongodb.github.io/node-mongodb-native/2.1/reference/connecting/connection-settings

reggiezhang avatar Apr 25 '18 12:04 reggiezhang