Too many connection errors when using mongodb with java

I am using MongoDbFactory to connect to mongodb with Java. But the mongo service throws a socket exception at least once per hour. Therefore, I am forced to restart the mongodb service to restore operations. I think this may be the result of unclosed connections with mongodb from java, and also MongoDbFactory does not provide me with a function to close the connection. How can I make sure all connections are closed after a specific session.

This is the code I'm using:

package com.####.mongo.configuration; import com.mongodb.Mongo; import org.springframework.context.annotation.Bean; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; @Configuration public class SpringMongoFeedConfig { public @Bean MongoDbFactory mongoDbFactory() throws Exception { return new SimpleMongoDbFactory(new Mongo(), "feedDatabase"); } public @Bean MongoTemplate mongoTemplate() throws Exception { MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), new MongoMappingContext()); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), converter); return mongoTemplate; } 

}

and

 private String insertFeedsToMongo(FeedMongoDTO feedObject, FeedType type) throws UnknownHostException { try { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoFeedConfig.class); MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate"); switch (type) { case FOLLOW: mongoOperation.save(feedObject, "feedsByUid"); break; case GENERAL: mongoOperation.save(feedObject, "allFeeds"); break; default: break; } return feedObject.getId(); } catch (Exception ex) { log.info("insertFeedsToMongo() : mongo Exception - ", ex); return null; } } 
+4
source share
2 answers

I am having problems with a Mongo object. Be sure to call myMongo.close() when this particular connection is allowed.

I would suggest storing instances created using new Mongo() in your factory function somewhere, which will allow it to be closed later (maybe there could be a SimpleMongoDbFactory for this). Just noted that Spring is -thing. The suggestion remains: to track your instances of Mongo .

+2
source

With the akaIDIOT suggestion, I did the following:

 public class FeedMongoOperations { public static transient Log log = LogFactory.getLog(FeedMongoOperations.class); private Mongo mongo; private SimpleMongoDbFactory dbFactory; private MongoTemplate mongoTemplate; public boolean openDbConnection() { try { MongoOptions options = new MongoOptions(); options.connectionsPerHost = 100; mongo = new Mongo("localhost", options); dbFactory = new SimpleMongoDbFactory(mongo, "feedDatabase"); MappingMongoConverter converter = new MappingMongoConverter(dbFactory, new MongoMappingContext()); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); mongoTemplate = new MongoTemplate(dbFactory, converter); return true; } catch (Exception e) { return false; } } public boolean closeDbConnection() { try { mongoTemplate = null; dbFactory = null; mongo.close(); return true; } } public String save(FeedMongoDTO feed, String collectionName) { try { mongoTemplate.save(feed, collectionName); return feed.getId(); } catch (Exception ex) { return null; } } public FeedMongoDTO getFeed(String mongoId, String collectionName) { try { FeedMongoDTO feedMongoDTO = mongoTemplate.findOne(new Query(Criteria.where("id").is(mongoId)), FeedMongoDTO.class, collectionName); return feedMongoDTO; } catch (Exception ex) { return null; } } } 
+1
source

All Articles