2

We are currently storing performance and search stats from our app with a free MongoDB sandbox on MongoLab. They recently had an outage which in turn affected our app (there is no replica set on the free sandbox).

Is there anyway to check the status of a MongoDB connection quickly (without having to waiting for a timeout as this takes too long) before trying to insert or update any data?

i3arnon
  • 113,022
  • 33
  • 324
  • 344
Gaz
  • 1,249
  • 3
  • 19
  • 37
  • What are you trying to accomplish? Solid error handling requires to handle any type of error, not only foreseeable ones.. What's the problem with a timeout in a web app? – mnemosyn Apr 23 '14 at 10:54
  • I'm basically trying to see if the server is alive. At present if I wait till connection times out it takes far too long and leaves the app unresponsive. I3arnon's answer below is exactly what I was looking for. – Gaz Apr 23 '14 at 15:51
  • I see. Keep in mind that you can still timeout within any DB operation, even if a ping just a ms before succeeded. – mnemosyn Apr 23 '14 at 16:03

1 Answers1

1

If by "detect status" you want to see whether you can access the server then use Ping:

new MongoClient("mongodb://localhost:27017").GetServer().Ping();

It throws an exception if the server cannot be reached.

i3arnon
  • 113,022
  • 33
  • 324
  • 344