I am using spring boot web application which connects to mongo db which is working out of the box. I just use the following properties:
spring.data.mongodb.h
The question is old but for all those who have problems with Mongo DB JAVA driver timeouts : set the maxConnectionIdleTime property.
Example Spring Boot :
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.annotation.PreDestroy;
import java.net.UnknownHostException;
@Configuration
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
@Autowired
private MongoProperties properties;
@Autowired(required = false)
private MongoClientOptions options;
@Autowired
private Environment environment;
private MongoClient mongo;
@PreDestroy
public void close() {
if (this.mongo != null) {
this.mongo.close();
}
}
@Bean
@ConditionalOnMissingBean
public MongoClient mongo() throws UnknownHostException {
this.options = options.builder().maxConnectionIdleTime(1000 * 60 * 4).build();
this.mongo = this.properties.createMongoClient(this.options, this.environment);
return this.mongo;
}
}
A very useful link here : Java DriverJAVA-2535 com.mongodb.MongoSocketWriteException ......