4

I am programming an UI where a user should be able to put in the URL and port to check whether a mongoDB server is running. Furthermore he should be able to provide credentials when necessary.

If the server is not running or the credentials are wrong, I want to provide a message for each case. Similar questions have been answered here:

Check MongoDB authentication with Java 3.0 driver

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

Unfortunately they use older versions of the Java driver for mongo. I'm using the 3.2+ version of the MongoDB java driver, where i.e. getDB() is deprecated.

My "solution" for the problem looks somewhat like this:

    try {
        String database = "test";
        MongoClient client = null;
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            MongoCredential credentials = MongoCredential.createCredential(username, database, password.toCharArray());
            client = new MongoClient(new ServerAddress(url, Integer.parseInt(port)), Arrays.asList(credentials));
        }
        else {
            client = new MongoClient(url, Integer.parseInt(port));
        }
        MongoDatabase db = client.getDatabase(database);
        db.listCollectionNames().first();
        client.close();
        return true;
    }
    catch (MongoCommandException | MongoSecurityException e) {
        // program does not get in here when credentials are wrong, 
        // only when no credentials are provided, but necessary
    }
    catch (MongoSocketOpenException | MongoTimeoutException e) {
        // only get in here after db.listCollectionNames().first() caused a     timeout
    }

How can I manage to:

  1. Find out when mongoDB server is not running?
  2. Find out that credentials are correct, when necessary?

Edit: When credentials are wrong (username and/or password) the method catches only the MongoTimeoutException. It's the same when the the wrong URL or port or database is provided. To be clear there are other exceptions printed out, but not caught. Only difference is, when providing no password and no username, even though the database requires them, then the MongoCommandException is caught

Community
  • 1
  • 1
st.huber
  • 1,481
  • 2
  • 24
  • 45
  • Please let me know if you need further information. I know that I could get the error cause out of the message of the `TimeoutException` and then find the specific exception cause, but I think this is a rather ugly solution. Any advice? – st.huber Oct 31 '15 at 19:29
  • 1
    Follow this solution = http://stackoverflow.com/questions/17194268/mongodb-check-connection-to-db/39294943#39294943 – Md Samiul Alim Sakib Sep 02 '16 at 14:39
  • @SakibSami Thank you for the link. Unfortunately it does not provide a solution for me. You need to call i.e. `client.getAddress()` to even get into the methods of `ServerMonitorListener`. The problem is, it still does not provide a way to check if MongoDB is up and running and that the credentials are correct. – st.huber Sep 12 '16 at 08:16

0 Answers0