1

My first time using Spring and I'm trying to develop an RESTful API. Using Spring Data MongoDB with MongoRepository, I would want to know why on my log are appearing up to 4 opened connections:

[  restartedMain] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
[127.0.0.1:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:1}] to 127.0.0.1:27017
[127.0.0.1:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=1907739}
[  restartedMain] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
[localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:27017
[localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=1145793}
[  restartedMain] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:3}] to localhost:27017
[  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
[  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
[ restartedMain] org.mongodb.driver.connection : Opened connection [connectionId{localValue:4, serverValue:4}] to 127.0.0.1:27017

My mongo configuration:

@Configuration
@EnableMongoRepositories(basePackages="com.api.repo")
class MongoConfig extends AbstractMongoConfiguration {

    @Value("${spring.data.mongodb.host}")
    private String url;
    @Value("${spring.data.mongodb.port}")
    private int port;
    @Value("${spring.data.mongodb.database}")
    private String db;

    @Override
    protected String getDatabaseName() {
        return db;
    }

    @Override
    public Mongo mongo() throws Exception {

//      return new Fongo("meh").getMongo();
        return new MongoClient(url, port);
    }

    @Override
    protected Collection<String> getMappingBasePackages() {
        return Arrays.asList("com.api");
    }
anat0lius
  • 2,145
  • 6
  • 33
  • 60
  • If I set MongoClient as singleton`private static final Mongo mongo = new MongoClient(url,port);` then I get Exception opening socket – anat0lius May 08 '17 at 08:15

2 Answers2

1

I have been trying with mongoOptions.

@Bean
    public MongoClientOptions mongoOptions() {
        return MongoClientOptions.builder().threadsAllowedToBlockForConnectionMultiplier(2).maxConnectionIdleTime(1).connectionsPerHost(1).minConnectionsPerHost(1).socketTimeout(2000).build();
    }

Playing with this configuration I am able to manage connections. In my case I am using spring boot and spring data so this is the only configuration I have apart from the application.properties

spring.data.mongodb.port=27017
spring.data.mongodb.database=your_database

Have in mind that if you are using for example 'mongobee' it will open new connections that you can´t manage from this configuration.

kimy82
  • 4,069
  • 1
  • 22
  • 25
  • Adding that I still getting the 4 connections. But I would like to know what are the sources for these connections. – anat0lius May 08 '17 at 10:45
0

Get into your mongo andmin and run db.serverStatus().connections You will see the open connections. Looks like it is no closing connections... have a look at This page in stackoverflow

Community
  • 1
  • 1
kimy82
  • 4,069
  • 1
  • 22
  • 25
  • App running: `{ "current" : 5, "available" : 51195, "totalCreated" : 9 }` App not running: `{ "current" : 1, "available" : 51199, "totalCreated" : 9 }`. I guess that I would need to make MongoClient as singleton – anat0lius May 08 '17 at 07:57