How to set Auto Reconnect with Unified Topology
Asked Answered
C

1

18

After setting useUnifiedTopology=true, the Auto Reconnect stopped working and generates the following ERRORS:

DeprecationWarning: The option `reconnectInterval` is incompatible with the unified topology
DeprecationWarning: The option `reconnectTries` is incompatible with the unified topology
DeprecationWarning: The option `autoReconnect` is incompatible with the unified topology

How can i get the server to auto-reconnect with that new flag?

I'm using mongoose.createConnection to connect with the following options:

{
        autoReconnect: true,
        keepAliveInitialDelay: 300000,
        connectTimeoutMS: 300000,
        reconnectTries: Number.MAX_VALUE,
        reconnectInterval: 1000,
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
        poolSize: 10,
        auth: {
            authSource: "admin"
        },
        user: process.env.MONGO_USER,
        pass: process.env.MONGO_PASS
    }
Charterhouse answered 24/12, 2019 at 8:10 Comment(0)
P
7

According to the docs you shouldn't usually set autoReconnect in combination with useUnifiedTopology Source: https://mongoosejs.com/docs/connections.html#options

autoReconnect - The underlying MongoDB driver will automatically try to reconnect when it loses connection to MongoDB. Unless you are an extremely advanced user that wants to manage their own connection pool, do not set this option to false.

Punctate answered 16/1, 2020 at 7:58 Comment(6)
I'm not using mongoose but having problem with the raw mongodb module, it doesn't allow me use autoReconnect=true: (node:4642) DeprecationWarning: The option autoReconnect is incompatible with the unified topology, please read more by visiting http://... (node:4642) DeprecationWarning: The option reconnectTries is incompatible with the unified topology, please read more by visiting http://...Amarelle
Mongoose uses the mongodb nodejs driver underneath. So the same principles apply :)Punctate
yeah but the new unified topo doesn't allow the old options 'autoReconnect' and 'reconnectTries'Amarelle
That's exactly my point :) You shouldn't set these properties according to the docs. You should implement your own reconnect tries. See this library for an example: github.com/nestjs/mongoose/blob/master/lib/…Punctate
This is braindead. So now tens of thousands will have reimplement own reconnection logic and create many custom buggy reconnection logics ...Automate
It seems that latest versions of MongoDB and moongoose drivers automatically try to reconnect when it loses connection to MongoDB so no need for the autoreconnect optionView

© 2022 - 2024 — McMap. All rights reserved.