0

I have a Node.js application which connects once (to a remote db) on application start and uses that connection variable for all operations.

MongoClient.connect(mongoConnStr, connOpts, function(err, db) {
    ...
}

The connOpts is:

var connOpts = {
    server: { 
        socketOptions: {
            autoReconnect: true, 
            keepAlive: 1 
        }
    }
};

So it connects well, but on request after being idle for about ~15mins, it hangs for ~30secs and then throws following error:

/home/me/Desktop/turbo/node_modules/mongodb/lib/utils.js:97
    process.nextTick(function() { throw err; });
                                        ^
MongoError: server myapp.cloudapp.net:27017 sockets closed
    at null.<anonymous> (/home/me/Desktop/turbo/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:301:47)
    at g (events.js:199:16)
    at emit (events.js:110:17)
    at null.<anonymous> (/home/me/Desktop/turbo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:101:12)
    at g (events.js:199:16)
    at emit (events.js:110:17)
    at Socket.<anonymous> (/home/me/Desktop/turbo/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:139:12)
    at Socket.g (events.js:199:16)
    at Socket.emit (events.js:107:17)
    at TCP.close (net.js:485:12)

I know that periodically pinging the db should resolve the problem, but maybe some configuration tweaks that I'm missing will do it?

moriesta
  • 1,170
  • 4
  • 19
  • 38

1 Answers1

0

Try something like this:

    options: {
        server:{
            auto_reconnect: true,
            socketOptions:{
                connectTimeoutMS:3600000,
                keepAlive:3600000,
                socketTimeoutMS:3600000
            }
        }
    }

I have found the code here.

Community
  • 1
  • 1
zag2art
  • 4,869
  • 1
  • 29
  • 39