Example usage for com.mongodb MongoOptions getConnectionsPerHost

List of usage examples for com.mongodb MongoOptions getConnectionsPerHost

Introduction

In this page you can find the example usage for com.mongodb MongoOptions getConnectionsPerHost.

Prototype

public synchronized int getConnectionsPerHost() 

Source Link

Document

Gets the maximum number of connections allowed per host for this Mongo instance.

Usage

From source file:com.seer.datacruncher.jpa.dao.MongoDbDao.java

License:Open Source License

/**
 * Gets connection to data base./*from  w ww  . j  a v  a  2 s.c  om*/
 * Note: mongoPool is thread-safe.
 *
 * @return connection
 */
private static DB getConnection() {
    try {
        if (mongoPool == null) {
            mongoPool = new Mongo(contactNode, Integer.parseInt(defaultPort));
            MongoOptions mo = mongoPool.getMongoOptions();
            if (mo.getConnectionsPerHost() <= 0 && !StringUtils.isEmpty(poolSize)) {
                mo.connectionsPerHost = Integer.parseInt(poolSize);
            }
        }
        DB mongoDB = mongoPool.getDB(MONGO_KEY_SPACE);
        if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) {
            mongoDB.authenticate(userName, password.toCharArray());
        }
        return mongoDB;
    } catch (NumberFormatException e) {
        log.error("Number format exception", e);
    } catch (UnknownHostException e) {
        log.error(e);
    }
    return null;
}