Monday, May 2, 2016

In MongoDB, how to make sure all connection will be closed if close() might not get called sometimes?

Leave a Comment

I am using node.js & MongoDB, and I have asynchronous code (async.queue) to update MongoDB.

When all the tasks in the queue is finished, I call

db.close()   // db is a Mongo client instance 

hence all connections used by the tasks are closed. However, in rare condition, I found there are open connections that are never closed seen in mongoDB logs. So after a few weeks, there will be hundred of connections never closed.

I researched and found that maybe I should set this option maxIdleTimeMS, but this option is not supported by all drivers (node.js driver does not support this option).

Even if I can fine tune my code to make sure there is no such condition that close() is not called. I am still wondering what if an app does not call db.close() for some reason (or as an extra insurance), is there any way to prevent hanging connections in MongoDB?

1 Answers

Answers 1

nodejs mongodb driver drops connection when idle

Handling MongoDB disconnect/reconnects from Node

looks like after some time of idle, connection is closed.

It is not clear which driver is used, but in one of that post there is link to docs:

http://mongodb.github.io/node-mongodb-native/api-generated/server.html#server

Set keepAlive to 0, and your connection will be terminated...

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment