Jason Bourne Jason Bourne - 4 years ago 175
Java Question

Can not Connect to MLab Mongo Database from mongodb java driver

This is my MLab setup for my blooddb databaseenter image description here

I am trying to connect it from a spring application by mongodb java driver.
This is my springDataDb Utils file:

public class SpringDataDBUtils {

private static MongoOperations mongoOperation;
private final static Properties properties = new Properties();
private final static Logger logger = LoggerFactory.getLogger(SpringDataDBUtils.class);

public static MongoOperations getMongoOperations() throws Exception {

if( mongoOperation==null){

logger.info("Connecting to db ... ");

MongoClientURI uri = new MongoClientURI(getDatabaseURI()+getDatabaseName());
MongoClient client = new MongoClient(uri);

mongoOperation = new MongoTemplate(client, getDatabaseName());
logger.info("Connected to db : "+ getDatabaseName());

}
return mongoOperation;

/*AppConfig appConfig = new AppConfig();

return appConfig.getMongoOperations();*/
}

protected static String getDatabaseName() {

try {
InputStream inputStream = SpringDataDBUtils.class.getClassLoader()
.getResourceAsStream(AppConstant.PROPERTIES_FILE);
properties.load(inputStream);

} catch (IOException e) {

logger.error("Error:"+e.getMessage());
}

return properties.getProperty(AppConstant.PROPERTIES_DB_NAME);
}



protected static String getDatabaseURI() {

try {
InputStream inputStream = SpringDataDBUtils.class.getClassLoader().getResourceAsStream(AppConstant.PROPERTIES_FILE);
properties.load(inputStream);

} catch (IOException e) {
logger.error("Error:"+e.getMessage());
}

String dbURI = "mongodb://"+ properties.getProperty(AppConstant.PROPERTIES_DB_USER) +
":" + properties.getProperty(AppConstant.PROPERTIES_DB_PASSWORD) +
"@" + properties.getProperty(AppConstant.PROPERTIES_DB_IP) +
":" + properties.getProperty(AppConstant.PROPERTIES_DB_PORT) + "/";

logger.info(dbURI);

return dbURI;
}

public static Properties ssProperties(){
try {
InputStream inputStream = SpringDataDBUtils.class.getClassLoader()
.getResourceAsStream(AppConstant.PROPERTIES_FILE);
properties.load(inputStream);

} catch (IOException e) {

logger.error("Error:"+e.getMessage());
}
return properties;
}
}


and my properties file is:

db.name=blooddb
db.password=****
db.user=****
db.ip= mongodb://<dbuser>:<dbpassword>@ds037587.mlab.com:37587/blooddb
db.port=27017


But while running the app i am getting exception.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/istiak/blooddb/AppConfig.class]: Bean instantiation via factory method failed


this is probably the db.ip i have provided here in the properties file.
so what can i do while putting ip from mLab?

Answer Source

Seems like there is problem in your dbURI string. You are adding "mongoldb://" two times in dbURI, one the constant string and other as computed from db.ip in properties file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download