나는 클래스에서 정적 변수로 DB를 설정하고 모든 CRUD 작업을 수행 할 때 모든 메서드에서이 db 변수를 사용하는 프로그램을 작성하고 있습니다.com.mongodb.DB.authenticate (String, String)가 메모리 인증에 있거나 mongo db를 호출합니까?
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;
}
}
질문이 코드에 입력되었습니다.
응답 해 주셔서 감사합니다. 하나의 설명이 필요합니다. MongoClient.getDB (dbName)를 사용하여 DB 인스턴스를 가져올 때 MongoClient.getDB (dbName)가 메모리에 자격 증명을 가져 오지 않습니까? –
@PradeepKrKaushal'getDB'는 MongoDB에 접속할 필요조차 없습니다. 인증은 DB로 실제로 작업을 수행 할 때만 필요합니다. – Philipp