Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect not working when using find #294

Open
jasonmcaffee opened this issue Mar 9, 2016 · 7 comments
Open

Reconnect not working when using find #294

jasonmcaffee opened this issue Mar 9, 2016 · 7 comments

Comments

@jasonmcaffee
Copy link

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

@saintedlama
Copy link
Collaborator

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.

@jasonmcaffee
Copy link
Author

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);
});

@yonatan-py
Copy link

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)

@geekbleek
Copy link

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.

@jsricarde
Copy link

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

@dsin
Copy link

dsin commented Apr 2, 2017

I also face this problem. Any suggestion on this ?

@reggiezhang
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants