-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
Empty show more show less
The MongoDb driver, sometimes, does not reconnect automatically. My application simply just stop working because the driver didn't reconnected. If I restart the application it works properly.
I've made a simples script to test it.
const mongodb = require('mongodb'); const MongoClient = mongodb.MongoClient; function randomIndex() { return Math.ceil(Math.random() * 2100); } MongoClient.connect('mongodb://localhost/somedb').then(db => { let collection = db.collection('somecollection'); db.on('close', err => console.log('close', err)); db.on('reconnect', () => console.trace('reconnect')); setInterval(() => { collection.find({ index: randomIndex() }).limit(1).next() .then(console.log) .catch(console.log); }, 1000); });
Steps to reproduce the error:
- Start mongodb
- Start script
- Stop mongodb
- Restart mongodb
- Stop mongodb
- Restart mongodb
After restarting it for the second time, my application running on node does not reconnect to mongo.
I think I have found the solution for this and made a pull request https://github.com/christkv/mongodb-core/pull/78.
I would like you to check if my solution is valid.
Thanks