Mongodb超时异常表示连接被拒绝

时间:2015-03-02 07:31:50

标签: java mongodb playframework spring-data-mongodb

请帮帮我。 我长期使用mongo DB进行应用程序。应用程序处于开发模式。有时候我会看到这个错误

  

ERROR-   无法调用该操作,最终得到一个错误:org.springframework.dao.DataAccessResourceFailureException:等待连接后10000 ms后超时。集群状态的客户端视图是{type = Unknown,servers = [{address = localhost:27017,type = Unknown,state = Connecting,exception = {com.mongodb.MongoException $ Network:Exception打开套接字},由{java引起.net.ConnectException:连接被拒绝}}];嵌套异常是com.mongodb.MongoTimeoutException:等待连接后10000 ms后超时。集群状态的客户端视图是{type = Unknown,servers = [{address = localhost:27017,type = Unknown,state = Connecting,exception = {com.mongodb.MongoException $ Network:Exception打开套接字},由{java引起.net.ConnectException:连接被拒绝}}]

但是当我重新启动服务器时,它不会再来了。但是,有一次我必须重新启动服务器。我使用的是mongodb 2.6.6。

请帮帮我。我害怕未来,因为很快就会出现在现场模式中。 我的Databaseconnection类:

static final String mongoServer = Play.application().configuration().getString("application.mongo.db.server");
static final String mongoDBname = Play.application().configuration().getString("application.mongo.db.dbname");
static final String mongoUsername = Play.application().configuration().getString("application.mongo.db.username");
static final String mongoPassword = Play.application().configuration().getString("application.mongo.db.password");
static final int connectionPerHost = Play.application().configuration().getInt("application.mongo.db.connections");
static final int connectionIdleTime = Play.application().configuration().getInt("application.mongo.db.idletime");

private MongoTemplate _mongoTemplate;
private static MongoClient   _mongo;

public MongoTemplate getContext() {
    if(_mongoTemplate == null)
        openDbConnection();
    if(_mongo == null || _mongoTemplate == null)
        Logger.error("DatabaseConnection::openDbConnection - Unable to get context. How is this possible?");
    return _mongoTemplate;
}

private static synchronized void createMongo() {
    if(_mongo == null) {
        Logger.debug("DatabaseConnection::openDbConnection - Opening a new connection");
        MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(connectionPerHost)
                .cursorFinalizerEnabled(true).maxConnectionIdleTime(connectionIdleTime).build();
        MongoCredential credential = MongoCredential.createMongoCRCredential(mongoUsername, mongoDBname, mongoPassword.toCharArray());
        ServerAddress addr = null;
        try {
            addr = new ServerAddress(mongoServer);
        } catch (UnknownHostException e) {
            Logger.error("Error Connecting to Mongo: Wrong Server??", e);
            e.printStackTrace();
        }
        _mongo = new MongoClient(addr, Arrays.asList(credential), options);
    }
}

private boolean openDbConnection() {
    try {
        if(_mongo == null) createMongo();
        // TODO: Connection Pooling
        _mongoTemplate = new MongoTemplate(_mongo, mongoDBname); //new MongoTemplate(dbFactory, converter);
        return true;
    } catch (Exception e) {
        Logger.error("Error Opening Connection:", e);
        e.printStackTrace();
    }
    return false;
}

private boolean closeDbConnection() {
    try {
        _mongoTemplate = null;
        return true;
    } catch (Exception ex) {
        Logger.error("Error Closing", ex);
    }
    return false;
}

@Override
protected void finalize() throws Throwable {
    closeDbConnection();
    super.finalize();
}

1 个答案:

答案 0 :(得分:0)

通常在未建立monogdb连接时出现,请验证它。