MongoDb connection pool has been closed....

I’m trying to run an API that uses mongo for ES and query repo. I’m getting the following error (at the bottom of this log):

Aug 03, 2017 7:12:06 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Aug 03, 2017 7:12:06 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.16
Aug 03, 2017 7:12:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
Aug 03, 2017 7:12:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout=‘30000 ms’, maxWaitQueueSize=500}
Aug 03, 2017 7:12:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server localhost:27017 to client view of cluster
Aug 03, 2017 7:12:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:27017
Aug 03, 2017 7:12:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 6]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=714906}
Aug 03, 2017 7:12:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Discovered cluster type of STANDALONE
Aug 03, 2017 7:12:08 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:27017
Aug 03, 2017 7:12:12 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:2, serverValue:2}] to localhost:27017 because the pool has been closed.
Aug 03, 2017 7:12:12 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]

Process finished with exit code 1

The connection to mongo is successfully established. In RoboMongo I can see the new database it creates, the events and snapshot collections. But then API exits…

Here’s my config files:

@Configuration
public class EventStoreConfig {
    @Value("${mongodb.host}")
    private String mongoHost;

    @Value("${mongodb.db.name}")
    private String mongoDbName;

    @Value("${mongodb.events.collection.name}")
    private String eventsCollectionName;

    @Value("${mongodb.events.snapshot.collection.name}")
    private String snapshotCollectionName;

    @Bean
    public Serializer axonJsonSerializer() {
        return new JacksonSerializer();
    }
    
    @Bean
    public EventStorageEngine eventStorageEngine(){
        return new MongoEventStorageEngine(
                axonJsonSerializer(),null, axonMongoTemplate(), new DocumentPerEventStorageStrategy());
    }

    @Bean(name = "axonMongoTemplate")
    public MongoTemplate axonMongoTemplate() {
        MongoTemplate template = new DefaultMongoTemplate(mongoClient(), mongoDbName, eventsCollectionName, snapshotCollectionName);
        return template;
    }

    @Bean
    public MongoClient mongoClient(){
        MongoFactory mongoFactory = new MongoFactory();
        mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoHost)));

        // This didn't help :(
        //MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build();
        //
        //mongoFactory.setMongoOptions(options);

        return mongoFactory.createMongo();
    }
}

In last couple of releases, Mongo has been changing their API quite a lot. I’m not a Mongo expert, but it looks like the client is attempting to set up a connection to a cluster, but finds a server in standalone mode.

Instead of using the MongoFactory, you might as well directly create a MongoClient instance using their own API. There, instead of passing a list of a single node (ServerAddress), pass the ServerAddress directly. This prevents the behavior of attempting to connect to a replica set.

Allard

Hi Allard,

Thanks for the quick reply. I tried:

    @Bean
    public MongoClient mongoClient(){
//        MongoFactory mongoFactory = new MongoFactory();
//        mongoFactory.setMongoAddresses(Arrays.asList(new ServerAddress(mongoHost)));
//
//        MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build();
//
//        mongoFactory.setMongoOptions(options);
//
//        return mongoFactory.createMongo();
        return new MongoClient( "localhost" , 27017 );
    }


Aug 04, 2017 9:58:20 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Aug 04, 2017 9:58:20 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.16
Aug 04, 2017 9:58:20 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
Aug 04, 2017 9:58:21 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Aug 04, 2017 9:58:21 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:14}] to localhost:27017
Aug 04, 2017 9:58:21 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 6]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=660034}
Aug 04, 2017 9:58:21 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:15}] to localhost:27017
Aug 04, 2017 9:58:21 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:2, serverValue:15}] to localhost:27017 because the pool has been closed.
Aug 04, 2017 9:58:21 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]



Once again, the connection to mongo is successfully established. In RoboMongo I can see the new database it creates, the events and snapshot collections. But the API still exits... :(

Bruno

It looks like tomcat itself is immediately stopped. Maybe something in your Spring Boot configuration?

I have thought of that too and will remove all the mongo config and see if the api still exits.

It seams reasonable that the mongo connection would be closed once the db and collections are created, and then a new one be created for each request…

It ended up being a dependency from one of our common libs that needed to be excluded… Thanks for the tip.

I am facing the same problem. Which common lib you had to exclude to fix this? I am using 3.0.2 java driver.