1

I have a Grails 2.4.4 app that is using MongoLab as it's database. I was previously connecting to the db using a mongo plugin for GORM (see plugin here). That has been less then a good experience, so I am in the process of switching over to Morpia and away from GORM for mongodb.

I currently have morphia library version 1.0.1 as a compile time dependency in the project. I have created a user in Mongolab with username: username, and password: pass (not actual creds...)

I am attempting the connect to the database using MongoClient. I have been looking at the documentation (documentation here) and I have also referenced this question but it is using the old way of connecting.

Here is my error, even though I don't see a reason why this error is being thrown because the format of my string appears to be correct:

ERROR context.GrailsContextLoaderListener
- Error initializing the application: Error creating bean with name 'datastore': 
Cannot resolve reference to bean 'mongoClient' while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'mongoClient': Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; 
nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
Message: Error creating bean with name 'datastore': Cannot resolve reference to bean 'mongoClient' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoClient': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'mongoClient': 
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.mongodb.MongoClient]: Constructor threw exception; 
nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanInstantiationException: Could not instantiate bean class [com.mongodb.MongoClient]: 
Constructor threw exception; nested exception is com.mongodb.MongoException: host and port should be specified in host:port format
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by MongoException: host and port should be specified in host:port format
->>  122 | <init>    in com.mongodb.ServerAddress
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|     49 | <init>    in     ''
|    118 | <init> .  in com.mongodb.MongoClient
|    262 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread
| Error Forked Grails VM exited with error

Here is a code snipplet that I have in resources.groovy where I am attempting the register the MongoClient, Morphia, and my datastore as spring beans.

beans = {

String databaseUrl = "mongodb://username:pass@ds053251.mongolab.com:12345/trendapp" //fake port
String databaseName = "trendapp"

mongoClient(MongoClient, databaseUrl)
morphia(Morphia)

datastore(Datastore, ref('mongoClient'), databaseName) { bean ->
    bean.factoryBean = 'morphia'
    bean.factoryMethod = 'createDatastore'
}
}

Any help is appreciated, thank you.

Community
  • 1
  • 1
hippofluff
  • 83
  • 1
  • 8

2 Answers2

6

You should provide the MongoClient with a MongoClientURI

MongoClientURI mongoClientURI = new MongoClientURI("connection string");
MongoClient mongoClient = new MongoClient(mongoClientURI);
Michael Freeman
  • 330
  • 1
  • 4
  • 14
0

So I did not figure out why this error was being thrown, but I was able to connect to my database by using a different constructor of the MongoClient, specifically the one that takes (ServerAddress, List<MongoCredential>).

hippofluff
  • 83
  • 1
  • 8