Reconnect not working when using find
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
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.
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);
});
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)
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.
Hi, I have the similar error it's necessary restart node app for reconnect with db.
I also face this problem. Any suggestion on this ?
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