I cannot insert a document object with spring-data-mongodb. I have configured MongoDB in my spring-mvc project like this:
@Configuration
@EnableMongoRepositories(basePackages = { "com.example.store.repository" })
public class MongoConfiguration extends AbstractMongoConfiguration {
@Bean
public Mongo mongo() throws UnknownHostException {
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
MongoCredential credential = MongoCredential.createMongoCRCredential("username", "store", "password".toCharArray());
MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(4).socketKeepAlive(true).build();
Mongo mongo = new MongoClient(serverAddress, Arrays.asList(credential), options);
return mongo;
}
@Bean(name = "MongoTemplate")
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongo(), "store");
}
@Override
protected String getDatabaseName() {
return "store";
}
}
I have added repository and the document as well. In one of my controller I insert a dummy document like this:
@RequestMapping(value="/add", method=RequestMethod.GET)
public String addProduct() {
Product product = new Product();
product.setName("New Product");
product.setDescription("Product Description");
product.setUnitPrice(19.99);
productRepository.insert(product);
return "redirect:/";
}
When I enter the url corresponding this method, it takes a few seconds and gives this error:
Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}]
I cannot reveal the problem. I have a user configured above and I am able to do write and read queries in mongo shell as that user. However, it fails through spring. Why?
MongoTemplatein your context. The one declared as@Bean(name = "MongoTemplate")will not be used within the repositories. Though this should not cause the error you encounter... So please try removing that manually declared bean, unless it is used with a@Qualifiersomewhere in your code, and give it a try then. Have you got a small sample reproducing the error you can point me to? Which MongoDB server, mongo-java-client and Spring-Data-MongoDB versions are you using?