0

I am using MongoDB with java. I need a way to check if MongoDB is running (to prevent exceptoins later). I tried it with this code:

mongoClient = new MongoClient();
try {
    mongoClient.getAddress();
    mongoRunning = true;
} catch (Throwable e){
    log.warn("no db connected");
    return;
}

I know throwable is bad but it is not working neither with exception nor with Throwable. I see (the catch block is not entered!):

[INFO ] 2018-11-10 22:33:26.209 [cluster-ClusterId{value='5be74e9f7170312fd4eb1ffe', description='null'}-127.0.0.1:27017] cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:?]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:?]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongo-java-driver-3.8.2.jar:?]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_102]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[?:1.8.0_102]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_102]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_102]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_102]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.8.0_102]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_102]
    at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_102]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.8.2.jar:?]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongo-java-driver-3.8.2.jar:?]
    ... 3 more

And my program terminates. An i thought i know java :-(. But i also tried:

mongoClient.listDatabases();

This behaves also odd: mongoRunning is set to true and an exception is thrown asynchronously. I need a way to check the connection once. Is this possible without any exception and in a synchronous way?

2
  • Have you tried to put the constructor call in the try block? Commented Nov 10, 2018 at 21:58
  • no, but the constructor never throws exception. Commented Nov 10, 2018 at 22:11

1 Answer 1

1

MongoClient represents a connection pool. It manages the connections, and everything happening in its threads.

I think it is wrong to ask question "if MongoDB is running". Suppose you have a tool or instrument to check if it is running, and you receive no exception at the time of checking, but right after you checked MongoDB is crashed, what would you do. I.e. it is wrong to attempt such check, instead one should focus to write application in reliable way with proper error handling at the time of queries.

Additionally, you may look at ConnectionPoolSettings which has getMaxWaitQueueSize, by default its 500 queries before it starts throwing exceptions. If that is what causes problems to you, you may try to reduce this to lesser value, to start receiving exceptions earlier.

Sign up to request clarification or add additional context in comments.

2 Comments

my use case is: the mongodb is optional to my application and if db is not available it just should not try to persist stuff/ se the db. i know about the listener i could use to get live signs of mongo. but for the moment i only need one synchronous check
If it is optional for application, I would do it on application configuration level. For instance, if MONGODB_URL is provided in environment variable, then do persistence, if not don't do. But having both, configured the connection URL, and sometimes doing and sometimes not doing persistence, it is not optional, it is something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.