I have been battling to access MongoDB collections in my openshift web application from my Java client, and failing at every turn. I can connect but cannot query the collections in any way.
Here is the current error message:
JBWEB000070: exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth fails' on server 127.12.158.2:27017. The full response is { "code" : 18, "ok" : 0.0, "errmsg" : "auth fails" }}}]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
JBWEB000071: root cause
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth fails' on server 127.12.158.2:27017. The full response is { "code" : 18, "ok" : 0.0, "errmsg" : "auth fails" }}}]
com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:167)
com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
com.mongodb.Mongo.execute(Mongo.java:760)
com.mongodb.Mongo$2.execute(Mongo.java:747)
com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:135)
com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:36)
org.jboss.tools.example.springmvc.mongodb.model.util.MongodbUtil.getCataloguesWithoutJoins(MongodbUtil.java:184)
The code I use to obtain the mongoClient that is used throughout the MondodbUtil class mentioned in the stacktrace is:
public static MongoClient getMongoClient() {
// If mongoClient not yet set up
if (mongoClient == null) {
mongoClient = new MongoClient(new MongoClientURI(
"mongodb://"+System.getenv("OPENSHIFT_MONGODB_DB_USER")+":"+
System.getenv("OPENSHIFT_MONGODB_DB_PASSWORD")+"@"+
System.getenv("OPENSHIFT_MONGODB_DB_HOST")+":"+
System.getenv("OPENSHIFT_MONGODB_DB_PORT")+"/jbosseap"));
}
return mongoClient;
}
I have also tried:
public static MongoClient getMongoClient() {
// If mongoClient not yet set up
if (mongoClient == null) {
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"),
Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
MongoCredential.createMongoCRCredential(
"OPENSHIFT_MONGODB_DB_USER",
"jbosseap",
"OPENSHIFT_MONGODB_DB_PASSWORD".toCharArray()
)
);
mongoClient = new MongoClient( seeds, credentials );
}
return mongoClient;
}
And also:
public static MongoClient getMongoClient() {
// If mongoClient not yet set up
if (mongoClient == null) {
mongoClient = new MongoClient(
new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"),
Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));
}
return mongoClient;
}
I have run out of ideas now, so seeking your wisdom!