Reputation: 1460
I'm looking for best way to check connection to Mongo DB. Situation: client makes request (api) to server. And server returns status of all databases.
What the best way to do it?
Upvotes: 10
Views: 40165
Reputation: 4917
The ping command is a no-op used to test whether a server is responding to commands. This command will return immediately even if the server is write-locked:
try {
DBObject ping = new BasicDBObject("ping", "1");
mongoTemplate.getDb().getMongo().getDB("DATABASE NAME").command(ping);
} catch (Exception exp) {
// MongoDb is down..
}
Upvotes: 6
Reputation: 659
Answering: How do I check that my MongoClient
connected to my db?
As has already been pointed out, the getting started documentation uses the ping command.
Packaged as a function:
/**
* Try to detect if the given MongoDatabase successfully connected to a Mongo
* instance.
*
* Will block up to the client's socket settings timeout, e.g. 30000 ms.
*
* Returning true indicates that future uses of the given client will probably
* succeed,
* but the connection is not guaranteed as connection state may change.
*
* @param database
* @return the present connection state
*/
static boolean isConnected(MongoDatabase database) {
Bson command = new BsonDocument("ping", new BsonInt64(1));
try {
database.runCommand(command);
} catch (MongoTimeoutException e) {
return false;
}
return true;
}
/**
* Try to detect if the given MongoClient successfully connected to a Mongodb
* instance.
*
* Will block up to the client's socket settings timeout, e.g. 30000 ms.
*
* Returning true indicates that future uses of the given client will probably
* succeed,
* but the connection is not guaranteed as connection state may change.
*
* @param mongoClient
* @return the present connection state
*/
static boolean isConnected(MongoClient mongoClient) {
return isConnected(mongoClient.getDatabase("admin"));
}
mongoClient.listDatabaseNames().first();
would also work, but that would just run the listDatabases command against the admin database. So manually writing the ping command is slightly better.
After running a command, mongoClient.getClusterDescription()
will return a meaningful result, but it does not block or wait, so calling it right after creating a MongoClient instance will not return an actionable response.
If you need more control, look at the answer using ServerMonitorListener.
This answer is valid as of/for 'org.mongodb:mongodb-driver-sync:4.9.1'
Upvotes: 1
Reputation: 1114
In Java MongoDriver 3.3.0 use ServerMonitorListener to determine whether server is up and connected or not. Here is the example code,
public class ServerConnection implements ServerMonitorListener {
private MongoClient client;
public ServerConnection(){
try {
MongoClientOptions clientOptions = new MongoClientOptions.Builder()
.addServerMonitorListener(this)
.build();
client = new MongoClient(new ServerAddress("localhost", 27017), clientOptions);
} catch (Exception ex) {
}
}
@Override
public void serverHearbeatStarted(ServerHeartbeatStartedEvent serverHeartbeatStartedEvent) {
// Ping Started
}
@Override
public void serverHeartbeatSucceeded(ServerHeartbeatSucceededEvent serverHeartbeatSucceededEvent) {
// Ping Succeed, Connected to server
}
@Override
public void serverHeartbeatFailed(ServerHeartbeatFailedEvent serverHeartbeatFailedEvent) {
// Ping failed, server down or connection lost
}
}
Upvotes: 7
Reputation: 3328
I use this:
Builder o = MongoClientOptions.builder().connectTimeout(3000);
MongoClient mongo = new MongoClient(new ServerAddress("192.168.0.1", 3001), o.build());
try {
mongo.getAddress();
} catch (Exception e) {
System.out.println("Mongo is down");
mongo.close();
return;
}
Upvotes: 8
Reputation: 1105
If I understand your question correctly you want to get state returned via a web service call. You can write a function that invokes db.serverStatus()
and have it return the data. Check out the documentation here:
Upvotes: 2
Reputation: 32076
Use MongoClient
for Java, all the info you need is here...
http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/
Upvotes: 2