question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto reconnect doesn't work properly

See original GitHub issue

Mongoose 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:closed
  • Created 10 years ago
  • Comments:23 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
TimNZcommented, Jan 14, 2014

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

};
mongoose.connect(options.conn_string, mongoOptions, callback);
// mongoose.set('debug',true)

}

0reactions
tommedemacommented, Jan 14, 2014

Is this still a problem?

I’m not using replicasets and my applications seem to die whenever they disconnect from the Mongo server.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found