【问题标题】:Mongo db timeout exception saying connection refusedMongodb超时异常说连接被拒绝
【发布时间】:2015-03-02 07:31:49
【问题描述】:

请帮助我。 我长期以来一直在使用 mongo DB 作为应用程序。该应用程序处于开发模式。有时我会看到这个错误

错误- 无法调用动作,最终报错:org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting to connect.集群状态的客户端视图是 {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, 由 {java .net.ConnectException:连接被拒绝}}];嵌套异常是 com.mongodb.MongoTimeoutException: 等待连接时在 10000 毫秒后超时。集群状态的客户端视图是 {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, 由 {java .net.ConnectException:连接被拒绝}}]

但是当我重新启动服务器时,它不会再次出现。但是有一次我必须重新启动服务器。我正在使用 mongodb 2.6.6。

请帮助我。我害怕未来,因为它很快就会进入现场模式。 我的数据库连接类:

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

【问题讨论】:

  • 答案很可能在您的服务器日志中,所以请看那里。这里没有看到任何代码的有根据的猜测是您可能正在耗尽可用的连接。您的代码中的某些内容可能是通过创建比您需要的更多的连接或在没有其他池管理的情况下产生繁重的负载而导致的。查看服务器日志和您的代码以获取相关连接信息。 StackOverflow 是一个“编码”问答网站。所以通常我们需要代码来诊断问题。
  • @neil lunn.i 已经发布了我的代码,谢谢建议
  • 但是你明白了吗?关键是您可能会创建比您需要的更多的连接。这段代码似乎没有必要。潜水员应该处理一个“连接池”,你的注入配置应该只是使连接可用。似乎这里没有提到的“其余代码”实际上多次建立“连接”并且从未释放它们。如果您以这种方式对这部分进行编码,那么您似乎在不需要时多次调用“连接”。那是我的观点。您需要进一步查看并删除多余的调用。
  • @Neil lunn 好的,我还需要用于连接池的代码。我明白这一点。我将添加代码,您能否将链接粘贴到此处。这将非常有帮助。提前致谢。

标签: java mongodb playframework spring-data-mongodb


【解决方案1】:

一般在monogdb连接未建立时出现,请验证。

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2012-01-19
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多