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?
Sometimes we see a lot of these cleanup threads if MongoClient.close() is not called at the appropriate times.
For example:
There were originally some race conditions around cleaning up these threads, but this was fixed in version 2.6 of the Java driver.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With