0

I have server implemented using nodejs. According to this post, I have one single connection (using native MongoDB) in my index.js file which connects to the MongoDB on server start.

I am using forever, so each time the server crashes this module restarts it.

My question is: What will happen if my mongo connection will crash? How can I handle this situation? How can I detect the crash programmatically? Should I restart it, if yes, how?

splatte
  • 2,048
  • 1
  • 15
  • 18
Max Z
  • 21
  • 7
  • are you using mongoose.js? – Kiran Pagar Apr 01 '15 at 19:04
  • @Kiran Pagar No, i am using native mongodb ( MongoClient = require('mongodb').MongoClient). http://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html – Max Z Apr 01 '15 at 19:08
  • For mongoose, you can use something like this - http://stackoverflow.com/questions/29342904/error-no-open-connections-at-db-executequerycommand-node-js/29347440#29347440 - There must be some similar way to go about this with native too. – Kiran Pagar Apr 01 '15 at 19:10

1 Answers1

0

You can set auto-connect parameter as true.

app.use(session({
store: new MongoStore({
    // Basic usage
    host: 'localhost', // Default, optional
    port: 27017, // Default, optional
    db: 'test-app', // Required

    // Basic authentication (optional)
    username: 'user12345',
    password: 'foobar',

    // Advanced options (optional)
    autoReconnect: true, // Default

})

}));

FRizal
  • 448
  • 7
  • 15