4

I'm getting this info message:

19-October-24 08:05:53:481 INFO Thread-4 o.m.d.connection:71 - Closed connection [connectionId{localValue:35, serverValue:38}] to mongodb:27017 because the pool has been closed.

And also the following errors:

java.lang.IllegalStateException: The pool is closed at com.mongodb.internal.connection.ConcurrentPool.get(ConcurrentPool.java:137) at

java.lang.IllegalStateException: state should be: open at com.mongodb.assertions.Assertions.isTrue(Assertions.java:70)

This is how I'm creating the MongoClient, plain and simple:

@Bean
@Override
public MongoClient mongoClient() {
    return new MongoClient(host);
}

This SO answer suggests to set socketKeepAlive(true) but as far as I understand this method is deprecated as it's true by default.

  • I'm using MongoTemplate and MongoRepository.
  • The application (as mentioned above) is multi-threaded.

I'd like to understand what's the meaning of the error ? (i.e. Why would the pool ever be closed?).

Do I need to set/adjust some Spring-Boot parameters maybe? Do I need to build MongoClient differently?

5
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Oct 24, 2019 at 11:27
  • @NeilLunn, I'm aware of this but there's no one snippet code to capture the problem in. I did demonstrated how I'm initializing the MongoClient. It is worth mentioning the application is multi-threaded (heavily) and maybe that's part of the problem. Commented Oct 24, 2019 at 11:29
  • 2
    There needs to be a snippet. That's the point. It's your job to provide that small sample of code that reproduces the issue. Without it, well that "snippet" as a comment is actually the quotation from the close explanation text which appears when the question is actually placed on hold. Which it likely will be without that specific example. So it does help you to make the effort to show such an example which actually reproduces the problem. We cannot debug your entire application. Commented Oct 24, 2019 at 11:37
  • I can add additional info: I'm using MongoTemplate and MongoRepository. The application (as mentioned above) is multi-threaded. Anyway, I'd like to understand what's the meaning of the error (i.e. Why would the pool ever be closed?). Do I need to set/adjust some Spring-Boot parameters maybe? Do I need to build MongoClient differently? Commented Oct 24, 2019 at 12:06
  • This thread jira.mongodb.org/browse/JAVA-2609 suggests that Spring Boot tries to close MongoClient for some reason Commented Oct 24, 2019 at 12:26

1 Answer 1

5
+50

This error means your MongoDB connections are closed for some reason and you are trying to use this connection pool.

If you are using springs connection pool you can create your connection pool without spring and you can manage the connection when is closed. ( Like reconnection on errors )

If you are doing multithreaded operations change your MongoClient beans Scope and create it thread based. Mongoclient creates a connection pool in the background and gives already pooled connections to newly created clients so thread-based clients will not create connection on every autowire operation.

If you want to use the socketKeepAlive feature you need to give options like this:

MongoClientOptions options = MongoClientOptions.builder()
                .socketKeepAlive(false)
                .build();

MongoClient mongoClient = new MongoClient( "yourhost:mongoport" ,  options);
Sign up to request clarification or add additional context in comments.

3 Comments

I'm missing something. All I do is using mongoTemplate and mongoRepository (albeit from different threads). I don't use MongoClient directly. Why would I need to manage the connection pool myself?
Also, I'm missing what is the suggested solution. Is it the Scope change or the socketKeepAlive?
When i had this error i was created connection myself and tested the connection before autowire. I think your solution is this but i dont know your software architecture so you need to design your own bean creation and usage scenerio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.