node.js driver isConnected return true after connection is closed

184 views
Skip to first unread message

Fabio Percivaldi

unread,
Oct 21, 2019, 5:19:15 PM10/21/19
to mongodb-user
We are trying to understand how we can check whenever the db instance is down.
In order to do so we are trying with a local docker instance of Mongo, even after stopping the container the client.isConnected() function return true.
Again, the isConnected returns true after a client.close() is called.

How can we know if the db instance is not available? is there another way? running a generic query and catching the result error?

In the Attachment you can find and example of what i'm talking about:
mongo.isConnected() in the try catch returns true
usersCollection.countDocuments({}) returns three 

even after closing connection. 

Thank you for your responses


Selection_031.png

Kevin Adistambha

unread,
Nov 5, 2019, 6:13:14 PM11/5/19
to mongodb-user

Hi,

Using node driver 3.3.3, there is an issue NODE-2234 where isConnected will always return true if the connection option useUnifiedTopology: true is set. As of node driver 3.3.3, this issue is still ongoing.

However there are things I noticed in the code you posted:

  • You don’t use useUnifiedTopology in your options. This means that you’re not going to see the effect of NODE-2234, thus isConnected should return false when the MongoDB instance was killed (even with kill -9). I have confirmed that this is the case by running a simplified version of the code you posted.
  • You close the connection (and await until it’s closed) before the code hit the setTimeout. This would make isConnected return false since the connection was already closed when the timer is started. I also have confirmed this is to be the case.

So I’m not sure how you can see isConnected to return true in both cases. Could you confirm:

  1. You don’t use useUnifiedTopology in your actual code so we can rule out NODE-2234?
  2. Is closing the connection before hitting the setTimeout a deliberate decision?
  3. Are you using the latest driver (3.3.3)?

Best regards,
Kevin

Tommaso Allevi

unread,
Nov 19, 2019, 4:13:43 PM11/19/19
to mongodb-user
Hi,
Could you confirm?

Thanks,
Tommaso
Reply all
Reply to author
Forward
0 new messages