使用mongodb和java时连接错误太多

时间:2013-02-18 10:23:42

标签: java spring mongodb connection

我使用MongoDbFactory与Java连接到mongodb。但是mongo服务每小时至少抛出一次套接字异常。因此我被迫重启mongodb服务以恢复操作。我认为这可能是由于与java的mongodb的未关闭连接的结果,并且MongoDbFactory也没有为我提供关闭连接的功能。如何确保在特定会话后关闭所有连接。

这是我正在使用的代码:

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; 
} 

}

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;
    }
}

2 个答案:

答案 0 :(得分:2)

我也遇到了Mongo对象的问题。确保在特定连接完成后调用myMongo.close()解决了这个问题。

我建议将使用new Mongo()创建的实例存储在您的工厂函数中,以便稍后关闭SimpleMongoDbFactory可能就是那个地方)。刚刚注意到这是一个春天的事情。建议仍然存在:跟踪您的Mongo个实例。

答案 1 :(得分:2)

根据akaIDIOT的建议,我已完成以下工作

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;
    }
}

}