zhou_077
zhou_077
2017-08-10 08:28

mongo已经设置最大连接了,还是频繁报连接数不够

  • mongo
  • java
  • mongodb

这个是获取连接的代码

 public class MongoUtil {
    private static Mongo mongo = null;
    private static DB db = null;
    private final static int POOLSIZE = 5000;// 连接数量  
    private final static int BLOCKSIZE = 1000; // 等待队列长度 
    private static Log log = LogFactory.getLog(MongoUtil.class);

    /**
     * 根据名称获取DB,相当于是连接
     * 
     * @param dbName
     * @return
     */
    public static DB getMongoDB() {
        if (mongo == null) {
            // 初始化
            init();
            db = mongo.getDB(ConfigUtils.systemConf("mongo.db"));
            db.authenticate(ConfigUtils.systemConf("mongo.user"), ConfigUtils.systemConf("mongo.pwd").toCharArray());
        }
        return db;
    }   

    private static void init() {
        String host = ConfigUtils.systemConf("mongo.ip");// 主机名
        int port = new Integer("27017");// 端口
        // 其他参数根据实际情况进行添加
        try {
            mongo = new Mongo(host, port);
            MongoOptions opt = mongo.getMongoOptions();
            opt.connectionsPerHost = POOLSIZE;
            opt.threadsAllowedToBlockForConnectionMultiplier = BLOCKSIZE;
        } catch (UnknownHostException e) {
            log.error(e);
        } catch (MongoException e) {
            log.error(e);
        }
    }
}

频繁报:Out of semaphores to get db connection

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答