13

I want to be able to detect if a mongo server is available from the java driver for the purpose of reacting to any abnormal events as one would in JDBC land etc. It all works fine when the server is up but I am struggling to understand why it is so difficult to detect errors. I have a feeling its because the mongo client runs in a different thread and it doesn't re throw to me or something?

try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase db = mongoClient.getDatabase("mydb");
        // if db is down or error getting people collection handle it in catch block
        MongoCollection<Document> people = commentarr.getCollection("people");
    } catch (Exception e) {
        // handle server down or failed query here.
    }

The result is

INFO: Exception in monitor thread while connecting to server localhost:27017

With the resulting stack trace containing a few different exceptions which I have tried to catch but my catch blocks still didn't do anything.

com.mongodb.MongoSocketOpenException: Exception opening socket
Caused by: java.net.ConnectException: Connection refused

I am using the java mongodb driver 3.0.4, most posts I read are from an older API with hacks like MongoClient.getDatabaseNames() which throws a MongoException if errors, except this is deprecated now and replaced with MongoClient.listDatabaseNames() which doesn't have the same error throwing semantics.

Is there a way to just execute a mongo query from the java driver in a try catch block and actually have the exception caught?

reversebind
  • 1,216
  • 1
  • 14
  • 18
  • 1
    @Alexander Kondaurov Can you take a look at this answer ? http://stackoverflow.com/questions/40813060/how-to-catch-exception-when-creating-mongoclient-instance/40814641#40814641. If you are looking for something specific please add to the post. – s7vr Apr 06 '17 at 12:55

2 Answers2

0

In the new API, MongoException is a RuntimeException. You can either catch the generic MongoException or, I believe, listDatabaseNames() would end up throwing a MongoCommandException ultimately.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • Thanks, I tried the listed exceptions but unfortunately the exceptions aren't being caught still. – reversebind Jan 03 '16 at 00:49
  • did you try catching `MongoSocketOpenException`? That's the one you're actively seeing. Probably because mongo isn't actually running (or on that port). – evanchooly Jan 04 '16 at 15:21
  • 1
    Yeah, I have tried that one and about 10 others but none of them get caught in the catch blocks. So far to detect when server is down the only exception I can catch that works is the `MongoTimeoutException` which gets thrown however many ms you set via `MongoClientOptions.builder().serverSelectionTime()`. I am just going with this for the time being. – reversebind Jan 05 '16 at 05:35
  • this still doesn't catch the exception unfortunately – Kyle Luke Jan 29 '16 at 17:25
0

You can redirect System.err to a ByteArrayOutputStream buffer. If a runtime exception is thrown, it will be collected into the buffer. See an answer to a similar problem at: https://stackoverflow.com/a/47699292/7388679