Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the connection pool from closing with a java driver on a mongodb?

I'm in the middle of upgrading from java driver 2.12.3 to 3.3.0. Curiously it seems that the collection pool is suddenly "acting up".

My set up 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's used in hundreds of threads:

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

using functions like this:

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

And for every DB interaction I get such a 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 looks as if the connection pool is closed after just one interaction. But why? very puzzled Anyone an idea?

like image 743
Anders Bernard Avatar asked Sep 26 '16 12:09

Anders Bernard


People also ask

Does MongoDB close connection automatically?

1.3 close() method in the Mongo databaseThe server will automatically close the cursors that have no remaining results and the cursors that have been idle for a time. Here is what the query syntax will look like.

How does connection pooling work in MongoDB?

A connection pool is a cache of open, ready-to-use database connections maintained by the driver. Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe.

Should I keep MongoDB connection open?

It is best practice to keep the connection open between your application and the database server.

Does MongoDB have a JDBC driver?

Progress DataDirect's JDBC Driver for MongoDB offers a high-performing, secure and reliable connectivity solution for JDBC applications to access MongoDB data.


Video Answer


2 Answers

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

Look at the link. There are several method that can probably help you. Look into the timeout related methods for connection and connection pool.


EDIT: added the correct answer (it was in the comments below)

MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build(); 
MongoClient client = new MongoClient("host", options);
like image 173
satish chennupati Avatar answered Oct 14 '22 18:10

satish chennupati


MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build();

socketKeepAlive is now Deprecated.
It now defaults to true and disabling it is not recommended.

like image 27
Anurag Avatar answered Oct 14 '22 16:10

Anurag