Search code examples
javamongodbmongodb-javanosql

com.mongodb.DB.authenticate(String,String) is in memory authentication or it make a call to mongo db?


I am writing a program where I am setting DB as the static variable in the class and I am using this db variable in every method when I am performing any CRUD operation.

public final class MongoDBUtil {

    private static MongoClient ejClient = null;

    private static DB db = null;

    /**
     * Prevent this class to creating the instance.
     */

    private MongoDBUtil() {

    }

    public static DB getDB() {
        if (db == null) {
            String userName = Property.INSTANCE.get("mongo.username");
            String pwd = Property.INSTANCE.get("mongo.pwd");
            String dbName = Property.INSTANCE.get("mongo.database.name");

            createMongoClient();

            db = ejClient.getDB(dbName);

          //db.authenticate will make a call to mongo db database or it's in memory call?

            boolean auth = db.authenticate(userName, pwd.toCharArray());

            if (!auth) {
                throw new EJException("Authentication failed for mongo db.");
            }
        }
        return db;
    }
}  

The question has been put in the code.


Solution

  • User accounts and permissions are stored on MongoDB (in the database system, to be precise). This means in order to authenticate, your application must connect to the database.