mongodb打开连接问题 [英] mongodb open connection issue

查看:200
本文介绍了mongodb打开连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongo控制台中有以下日志:

I have the following log in my mongo console:

Tue Jul 23 17:20:01.301 [initandlisten] waiting for connections on port 27017
Tue Jul 23 17:20:01.401 [websvr] admin web console waiting for connections on port 28017
Tue Jul 23 17:20:01.569 [initandlisten] connection accepted from 127.0.0.1:58090 #1 (1 connection now open)
Tue Jul 23 17:20:01.570 [initandlisten] connection accepted from 127.0.0.1:58089 #2 (2 connections now open)
Tue Jul 23 17:20:21.799 [initandlisten] connection accepted from 127.0.0.1:58113 #3 (3 connections now open)
....
....
....

同样,日志继续进行,现在是112.每次我启动mongo服务器时,都会发生这种情况.我的代码中只有一个单例连接.这可能是什么问题:

likewise the log goes on and now it is in 112. Each time when i start mongo server this happens. I only have a singleton connection in my code. What can be the issue here:

public static DB getConnection(String databaseName) throws AppConnectionException {

    if (null != db) {
        Logger.debug("Returning existing db connection...!");
        return db;
    }

    Logger.debug("Creating new db connection...!");
    final String connStr = PropertyRetreiver.getPropertyFromConfigurationFile("rawdata.url");

    try {

        final MongoClientURI uri = new MongoClientURI(connStr);
        final MongoClient client = new MongoClient(uri);
        db = client.getDB(databaseName);

    } catch (UnknownHostException e) {
        throw new AppConnectionException(
                "Unable to connect to the given host / port.");
    }

    return db;
}

推荐答案

MongoClient具有内部连接池.可以配置最大连接数(默认为100).您可以像这样使用 MongoClientOptions 进行设置:

MongoClient has internal connection pool. Maximum number of connections can be configured (default is 100). You can set it by using MongoClientOptions like this:

MongoClientOptions options = MongoClientOptions.builder()
                .connectionsPerHost(100)
                .autoConnectRetry(true)
                .build();

然后将这些选项提供给MongoClient(在Mongo Java API v2.11.1中已选中).池中的连接保持打开状态(打开和关闭连接通常是一项昂贵的操作),以便以后可以重用它们.

And then give these options to MongoClient (checked it in Mongo Java API v2.11.1). Connections in pool are maintained open (opening and closing connection is usually an expensive operation) so that they can be later reused.

例如,我还将使用 enum 重构您的MongoDB客户端单例,以避免将 synchronized 放在此方法上.

I would also refactor your MongoDB client singleton using enum for example to avoid putting synchronized on this method.

这是我的意思的草图:

public enum MongoDB {
    INSTANCE;

    private static final String MONGO_DB_HOST = "some.mongohost.com";
    private Mongo mongo;
    private DB someDB;

    MongoDB() {

        MongoClientOptions options = MongoClientOptions.builder()
                .connectionsPerHost(100)
                .autoConnectRetry(true)
                .readPreference(ReadPreference.secondaryPreferred())
                .build();

        try {
            mongo = new MongoClient(MONGO_DB_HOST, options);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        someDB = mongo.getDB("someDB");
         //authenticate if needed
         //boolean auth = someDB.authenticate("username", "password".toCharArray());
         //if(!auth){
         //     System.out.println("Error Connecting To DB");
         //}        
    }

    public DB getSomeDB() {
        return someDB;
    }

    //call it on your shutdown hook for example 
    public void close(){
        mongo.close();
    }
}

然后,您可以通过以下方式访问数据库

Then, you can access your database via

MongoDB.INSTANCE.getSomeDB().getCollection("someCollection").count();

这篇关于mongodb打开连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆