Reputation: 794
I use MongoDbFactory to connect to mongodb with Java. But the mongo service throws socket exception at least once every hour. Hence I am forced to restart mongodb service to restore operations. I think that this may be a result of unclosed connections to mongodb from java and also MongoDbFactory do not provide me a function to close a connection. How can I make sure that all connections are closed after a particular session.
This is the code I am 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;
}
}
Upvotes: 4
Views: 3339
Reputation: 794
With akaIDIOT's suggestion I have done as 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;
}
}
}
Upvotes: 2
Reputation: 9231
I've encountered issues with the Mongo
object as well. Making sure to call myMongo.close()
when that particular connection is done resolved it.
I would suggest storing your instances created with new Mongo()
in your factory function somewhere that would allow it to be closed later (the Just noted that's a Spring-thing. Suggestion remains: track your SimpleMongoDbFactory
might be just the place for that).Mongo
instances.
Upvotes: 2