0

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?

2
  • the config above creates more than one bean of type MongoTemplate in 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 @Qualifier somewhere 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? Commented Nov 9, 2015 at 7:23
  • @ChristophStrobl actually sir, I do not know what is going on clearly. I try to make it work by using this article: viveksoni.net/…. I have mongodb3 running in the same machine. mongo-java-driver: 3.1.0. Commented Nov 9, 2015 at 17:18

1 Answer 1

2

Could you try this code as MongoConfiguration it might help you and let me know if it has solved your problem

@Configuration
@EnableMongoRepositories(basePackages = { "com.example.store.repository" })
public class MongoConfiguration extends AbstractMongoConfiguration {

  @Bean
  public Mongo mongo() throws UnknownHostException {
    return mongoClient();
  }

  @Bean
  public MongoDbFactory mongoDbFactory() {
    return new SimpleMongoDbFactory(mongoClient(), getDatabaseName());
  }

  @Bean
  public MongoTemplate mongoTemplate() {
    return new MongoTemplate(mongoDbFactory(), null);
  }

  @Override
  protected String getDatabaseName() {
    return "store";
  }

  @Bean
  public MongoClient mongoClient() {    
    List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
    credentialsList.add(MongoCredential.createCredential("username", getDatabaseName(), "password".toCharArray());
    ServerAddress primary = new ServerAddress("localhost", 27017);
    MongoClientOptions mongoClientOptions = MongoClientOptions.builder().connectionsPerHost(4).socketKeepAlive(true).build();
    return new MongoClient(Arrays.aslist(primary), credentialsList, mongoClientOptions); 
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.