how to check from a driver, if mongoDB server is running

我与影子孤独终老i 提交于 2019-12-18 03:56:26

问题


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?


回答1:


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.




回答2:


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) {
       ...
 }



回答3:


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) {
  ...
}



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/6832517/how-to-check-from-a-driver-if-mongodb-server-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!