Dima
Dima

Reputation: 4128

MongoDB - massive amount of MongoCleaner threads

Somehow my java application talking to mongodb ended up with a huge number of parked (sleeping) threads named MongoCleanerXXX which I presume come from the driver. Number of those was ~600. Apparently there were some connection problems with the database which did recover after some time after mongod was restarted.

MongoDB Java driver version is 2.10.1 MongoDB versions is 2.2.0

What could be the reason for this and what I should be doing wrong to cause this as a client app of MongoDB?

Upvotes: 5

Views: 3177

Answers (2)

Nicholas DiPiazza
Nicholas DiPiazza

Reputation: 10595

See http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/

The MongoClient class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given database cluster and use it across your application. If for some reason you decide to create many MongoClient instances, note that:

  • all resource usage limits (max connections, etc) apply per MongoClient instance
  • to dispose of an instance, make sure you call MongoClient.close() to clean up resources

Switch your MongoClient invocation to a singleton:

static public MongoClient mongoClientInstance = null;
public static synchronized MongoClient getMongoClientInstance(String host) {
    if (mongoClientInstance == null) {
        try {
            mongoClientInstance = new MongoClient(host);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return mongoClientInstance;
}

Seems to work great.

Upvotes: 3

Trisha
Trisha

Reputation: 3931

Sometimes we see a lot of these cleanup threads if MongoClient.close() is not called at the appropriate times.

For example:

  • When undeploying a webapp (see JAVA-817)
  • When authentication fails (see JAVA-831)

There were originally some race conditions around cleaning up these threads, but this was fixed in version 2.6 of the Java driver.

Upvotes: 4

Related Questions