17

I wonder, if there is a way to check if mongoDB server is running from java driver for mongoDB?

According to the tutorial, I can do

Mongo m = new Mongo();
// or
Mongo m = new Mongo( "localhost" , 27017 );
// and
DB db = m.getDB( "mydb" );

But how to check that I can use these Mongo and DB? I see no isConnected() method in the API.

db.getConnector().isOpen() 

returns true

The only way I found is call db.getDatabaseNames() and catch MongoException.

If there some more civilized approach?

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
zmila
  • 1,651
  • 1
  • 11
  • 13

5 Answers5

22

You can run a ping command

 Mongo mongo = new Mongo();
 DBObject ping = new BasicDBObject("ping", "1");
 try {
       mongo.getDB("dbname").command(ping);
 } catch (MongoException e) {
       ...
 }
Robert
  • 39,162
  • 17
  • 99
  • 152
Benoît Guérout
  • 1,977
  • 3
  • 21
  • 30
12

I've found this to be more direct than the ping command:

Mongo mongo = new Mongo();
try {
  mongo.getConnector().getDBPortPool(mongo.getAddress()).get().ensureOpen();
} catch (Exception e) {
  ...
}
Ted M. Young
  • 1,052
  • 11
  • 24
  • Dude, nothing, including ping command or anything could not help to handle java.net.ConnectException. This works like a charm! – tugcem Oct 06 '13 at 19:58
  • It looks like mongo.getAddress() returns null in case mongo is down, Do you really need these extra steps? – Amazia Gur Nov 18 '13 at 11:16
  • @amazia No. If you're using the latest version which uses `MongoClient` instance, `mongoClient.getAddress()` does *not* return `null`. Don't know about the earlier versions. This is actually the most effective method. +1 – Niks Feb 20 '14 at 13:39
  • 3
    The class DBTCPConnector returned by `getConnector()` is deprecated in v2.6 and was removed in Mongo v3.0. – Robert Oct 05 '15 at 09:22
11

if there is a way to check if mongoDB server is running from java driver for MongoDB?

So if you can do the following:

Mongo m = new Mongo( "localhost" , 27017 );
DB db = m.getDB( "mydb" );

Then you are connected to the database, otherwise that m.getDB() would be throwing an exception. If you can connect to the database, then the MongoDB server is running.

The only way I found is call db.getDatabaseNames() and catch MongoException. If there some more civilized approach?

Is there something specifically wrong with this approach?

The driver basically runs in a sandbox where it can or cannot connect. You're asking the driver to know something specific about the server (is process X running?), but that's not the driver's job. It can either connect or it can't, it's not responsible for operating the service/process, just for connecting to it.

To know that the process is actually running, you need administrative functions on that server that allow you to check that mongod is indeed running with the correct parameters.

buræquete
  • 14,226
  • 4
  • 44
  • 89
Gates VP
  • 44,957
  • 11
  • 105
  • 108
  • 1
    The problem is that this doesn't look as though it is throwing a checked exception - this case is something that can be handled by the caller. – wulfgarpro Nov 03 '11 at 04:28
  • If it's not throwing a checked exception in Java, that may actually be a bug. Have you checked jira.mongodb.org for a ticket? – Gates VP Nov 04 '11 at 06:20
  • 12
    Perhaps this question has become out of date, because on my current mongo version (2.7.3) I can call mongo.getDB("anynameatall") and it won't throw an exception, even when "connected" to a URL with no Mongo running (e.g. "mongodb://127.0.0.1:12345"). I too am stuck with the uncivilised getDatabaseNames() check. – Daniel Flower Apr 05 '12 at 13:43
  • @DanielFlower, not sure how the Java MongoDB driver has changed over time, but since I've been using it, Mongo.getDB() has never actually hit the server. The driver doesn't seem to bother connecting until it has to; getDB() is just setting up the driver to prepare for real work. – ericsoco Aug 20 '12 at 19:49
  • 3
    This approach is not working anymore. Must do : Mongo mongo = new MongoClient("localhost", 27017); DB test = mongo.getDB("test"); DBCollection coll = test.getCollection("test"); coll.findOne(); – Thomas Dec 11 '14 at 11:04
3
public boolean keepAlive(Mongo mongo) {
    return mongo.getAddress() != null;
}

This will return null for address if mongo is down. You can look within the implementation of getAddress() to see why it is a good way to check the mongo's status.

I assume you've initialized the mongo parameter properly.

Amazia Gur
  • 1,692
  • 17
  • 16
  • 1
    This does not return `null` even if MongoDB is down. It simply returns a valid com.mongodb.ServerAddress instance which is initialized with the server address and port we provide while initialize `Mongo mongo`. Or `MongoClient mongo` with newer version – Niks Feb 20 '14 at 13:34
  • You could try `mongo.getAllAddress()` – rupweb Nov 17 '15 at 13:27
1

I haven't tested this thoroughly (only using a localhost mongo) but it appears to work so far:

public boolean mongoRunningAt(String uri) {
    try {
        Mongo mongo = new Mongo(new MongoURI(uri));
        try {
            Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
            socket.connect(mongo.getAddress().getSocketAddress());
            socket.close();
        } catch (IOException ex) {
            return false;
        }
        mongo.close();
        return true;
    } catch (UnknownHostException e) {
        return false;
    }
}

And the tests I've used:

@Test
public void whenMongoNotAvailableAtSpecificURLThenTheLoaderKnows() {
    assertThat(mongoRunningAt("mongodb://127.0.0.1:12345"), is(false));
}

@Test
public void whenMongoAvailableAtSpecificURLThenTheLoaderKnows() {
    assertThat(mongoRunningAt("mongodb://127.0.0.1:27017"), is(true));
}

It's not exactly using a well defined public API so use at your own risk.

Daniel Flower
  • 687
  • 7
  • 13