How to save connection pool from closing using java driver on mongodb?

I am in the middle of updating from java driver 2.12.3 to 3.3.0. Curiously, it seems that the collection pool has suddenly “taken off”.

My setup is as follows:

The connection is established in the main thread:

mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
mongoClient.setWriteConcern(new WriteConcern(0, 10)); // deprecated, replace soon
database = mongoClient.getDatabase("Example");
// java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(Level.SEVERE);

It is used in hundreds of threads:

org.bson.Document oldDoc = DBInteractions.readOneFromDb("articles");

using the following functions:

static synchronized Document readOneFromDb(String col) {
    return database.getCollection(col).find().limit(1).sort(new Document().append("count", 1)).first();
}

And for every interaction with the database I get this warning:

Sep 26, 2016 2:33:19 PM com.mongodb.diagnostics.logging.JULLogger log
INFORMATION: Closed connection [connectionId{localValue:42, serverValue:248}] to localhost:27017 because the pool has been closed.

It appears that the connection pool is closed after only one interaction. But why? very puzzled Any idea?

+4
source share
1 answer

https://api.mongodb.com/java/3.1/com/mongodb/MongoClientOptions.html

. , . , .


EDIT: ( )

MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build(); 
MongoClient client = new MongoClient("host", options);
+4

Source: https://habr.com/ru/post/1655840/


All Articles