7

With mongoose one can simply handle reconnections via connection-options:

let dbOptions = {
    dbName: process.env.MONGO_DATABASE_NAME,
    autoReconnect: true,
    reconnectInterval: 1000,
    reconnectTries: 10,
};

mongoose.connect(process.env.MONGO_URI, dbOptions);
// Create connection object.
const db = mongoose.connection;

In the native MongoDB driver (version 4.4) there are no similar connection options available: https://mongodb.github.io/node-mongodb-native/4.4/interfaces/MongoClientOptions.html

What is the easiest way to handle database reconnections in case of a major error?

3
  • There is an option keepAlive and it is by default true, no need to change, your question is not clear, case of a major error > what kind error, add more details in your question. Commented Feb 23, 2022 at 6:30
  • @turivishal thanks for the response. Really appreciated. With major error I mean that if my database is down for a few minutes, I would like my node.js server to automatically try to reconnect. Commented Feb 23, 2022 at 12:10
  • 1
    it should reconnect, and i am not sure but that error is from node, so you have to handle error in nodejs. please post the error, and your sample connection code. Commented Feb 23, 2022 at 12:21

1 Answer 1

2
+200

Wherever you've got the mongoose connection snippet from, it's outdated.

The autoReconnect and auto_reconnect options were a thing in Nodejs native driver before v4.0, and mongoose just proxied these options to the driver.

This is the documentation for the driver 3.7 with "autoReconnect" still there: http://mongodb.github.io/node-mongodb-native/3.7/api/global.html#MongoClientOptions and this is the commit where it's been removed: https://github.com/mongodb/node-mongodb-native/commit/e3cd9e684aea99be0430d856d6299e65258bb4c3#diff-f005e84d9066ef889099ec2bd907abf7900f76da67603e4130e1c92fac92533dL90

The option was "True" by default with a strong note to do not change this value unless you know exactly why you need to disable it.

v4 introduced many changes to the driver - refactoring to typescript, architectural changes, you can see it from the commit, right. One of the changes affected connection logic and pool management. There is no option to disable reconnection anymore. It's always reconnects regardless how you connect directly or via mongoose.

Sign up to request clarification or add additional context in comments.

5 Comments

How many times will it try to reconnect? What if I start my server, turn of the database for 3 hours and then turn it on again? Will the node.js server be automatically reconnected without any additional code?
If you read the client options documentation you referred in the question, there are number of options that control different aspects of connectivity - connectionTimeoutMS, socketTimeoutMS, heartbeatFrequencyMS, wtimeoutMS etc. For your 3hrs blackout example - it will try to reconnect once on first operation. mongodb.github.io/node-mongodb-native/3.6/reference/… explains the idea and the rationale behind the changes. Starting from v4.0 they dropped support of legacy topologies.
This should be marked as the correct answer as when there is not a pooled connection active, the driver will try to connect to the database and perform the database operation you requested. If there is no data node available that operation will fail. If you are worried about an operation failing when a failover is happening i suggest leveraging retry-able read/writes found here: mongodb.com/docs/manual/reference/connection-string/…
@Scott, can't disagree =) I truly believe it's a correct answer. Frankly, I lost count how many times OP assigned the bounty. At the time of answering, it was "to get answer from reputable source" so I put few quite reputable links in the answer. This time it says "help me to solve the problem" but there is no clear problem statement, only a question what is an easiest way to reconnect. I doubt there is an easier way than doing nothing on application layer. Apparently it didn't solve OPs problem whatever it is, so I just gave up trying to help.
ok ok ok, you got the bounty. Thanks for your help @AlexBlex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.