Auto reconnect doesn't work properly
See original GitHub issueMongoose 3.6.9 doesn’t seem to have working auto-reconnect. Here is my test case against a local MongoDB server which I kill to test Mongoose’s reconnection support:
//
// Program to query a local mongodb every second. This should auto-reconnect.
//
// Instructions:
//
// 1) start mongod
// 2) npm i mongoose@3.6.9 && node reconnect-mongoose.js
// 3) kill mongod
// 4) after a short interval, restart mongod
//
// Mongoose (3.6.9) should auto-reconnect when mongod has restarted,
// but doesn't appear to.
//
/*
Sample output:
$ node reconnect-mongoose.js
mongodb query ok
mongodb query ok
mongodb query ok
mongodb query ok
(KILL mongodb in another term)
mongo connection error: [Error: failed to connect to [localhost:27017]]
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
(START mongodb in another term)
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
mongodb query not ok
*/
var mongoose = require('mongoose')
mongoose.model('user', new mongoose.Schema({ username: String, email: String }))
var mongoUrl = "mongodb://localhost/mydb"
mongoose.connect(mongoUrl, function(err) {
if (err) throw err
var users = mongoose.model('user')
mongoose.connection.on('error', function(e) {
console.log("mongo connection error: ", e)
})
// Run a simple 'find' query every second
setInterval(function() {
users.findOne(function(err, collection) {
if (err) {
return console.log("mongodb query not ok")
}
console.log("mongodb query ok")
})
}, 1000)
})
Issue Analytics
- State:
- Created 10 years ago
- Comments:23 (6 by maintainers)
Top Results From Across the Web
Turning Auto Reconnect On or Off on Mobile Devices - Webroot
Tap Auto Reconnect. In the Auto Reconnect area, do either of the following: Tap the slider to the left to turn Auto Reconnect...
Read more >Fix: Android Doesn't Automatically Connect to WiFi
Fix: Android Doesn't Automatically Connect to WiFi · #1 Restart your Android device · #2 Delete the saved networks · #3 Reconnect the...
Read more >iPhone Won't Auto-Connect to WiFi – How to Fix - Alphr
Turn your Wi-Fi off and on in the Settings app or the slide down menu. Make sure to wait for a few seconds...
Read more >[RESOLVED] Reconnect not always working
Always connectThe VPN always opens when the session is open... You are prompted twice to open KeePass when connecting to a session.In KeePass,...
Read more >Wifi disconnects and doesn't automatically reconnect
-Click Troubleshooter then Additional troubleshooter. -Look for Internet Connection and run the troubleshooter. Once done proceed with these ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This works on MS Azure VM. Auzre has annoying timeouts on ‘idle’ network connections, db instance hosted via Mongolab.
function initDb(options, callback) { var mongoOptions = { db: { safe: true }, server: { auto_reconnect: true, socketOptions: { keepAlive: 1 } }
}
Is this still a problem?
I’m not using replicasets and my applications seem to die whenever they disconnect from the Mongo server.