Example usage for com.mongodb MongoClient close

List of usage examples for com.mongodb MongoClient close

Introduction

In this page you can find the example usage for com.mongodb MongoClient close.

Prototype

public void close() 

Source Link

Document

Closes all resources associated with this instance, in particular any open network connections.

Usage

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected void update(DBObject query, DBObject object) {
    MongoClient client = getClient();
    try {//from  w  w w. j a v  a 2s.co m
        getCollection(client).update(query, object);
    } finally {
        client.close();
    }
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected <T> Iterable<T> find(DBObject query, DBObject fields, DBObject sort, Integer limit,
        Function<DBObject, T> transformer) {
    MongoClient client = getClient();
    try {/*from   ww w.  j av a 2s. c  om*/
        DBCursor cursor = fields == null ? getCollection(client).find(query)
                : getCollection(client).find(query, fields);
        if (sort != null) {
            cursor = cursor.sort(sort);
        }
        if (limit != null) {
            cursor = cursor.limit(limit);
        }
        List<T> result = new LinkedList<T>();

        try {
            while (cursor.hasNext()) {
                result.add(transformer.apply(cursor.next()));
            }
        } finally {
            cursor.close();
        }

        return result;
    } finally {
        client.close();
    }
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected DBObject findOne(BasicDBObject query) {
    MongoClient client = getClient();
    try {// w ww. ja  v  a 2 s. c  o  m
        DBCollection collection = getCollection(client);
        return collection.findOne(query);
    } finally {
        client.close();
    }
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected DBObject findOne(BasicDBObject query, DBObject fields, DBObject orderBy,
        ReadPreference readPreference) {
    MongoClient client = getClient();
    try {//  w  w w .  j  a v a  2  s.  c  o  m
        DBCollection collection = getCollection(client);
        return readPreference == null ? collection.findOne(query, fields, orderBy)
                : collection.findOne(query, fields, orderBy, readPreference);
    } finally {
        client.close();
    }
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected int size(BasicDBObject query) {
    MongoClient client = getClient();
    try {//from  w w w  .j a va 2  s.c  o m
        return getCollection(client).find(query).count();
    } finally {
        client.close();
    }
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected void delete(DBObject token) {
    MongoClient client = getClient();
    try {//from   w  ww .  ja v a 2s  .  c  o  m
        getCollection(client).remove(token);
    } finally {
        client.close();
    }
}

From source file:com.ibm.gaiandb.mongodb.MongoConnectionFactory.java

License:Open Source License

/**
 * Closes the Mongo Collection object - frees up any resources held by the collection and the related
 * connection object.//from   ww  w  .j  a v  a2s .  c  o m
 * 
 * @param connectionParams - object containing all the parameters needed to connect to MongoDB
 * @exception UnknownHostException - if we cannot connect to the mongoDB host specified
 * @exception AuthenticationException - if authentication with the mongoDB process fails.
 * 
 */
public static void closeMongoCollection(DBCollection mongoCollection) {
    //we have to close the mongo client object, get a reference to it via the database object.
    DB mongoDB = mongoCollection.getDB();
    MongoClient mongoClient = (MongoClient) mongoDB.getMongo();
    mongoClient.close();

}

From source file:com.ibm.research.mongotx.daytrader.Load.java

License:Open Source License

public static void main(String[] args) throws Exception {
    //MongoClient client = new MongoClient(new MongoClientURI("mongodb://admin:admin@ds043714.mongolab.com:43714/?authSource=trade&authMechanism=SCRAM-SHA-1"));
    MongoClient client = new MongoClient("localhost");
    TxDatabase txDB = new LatestReadCommittedTxDB(client, client.getDatabase("trade"));
    dropCollections(txDB);// w  ww  .  j a v  a 2 s. com
    populate(txDB);
    client.close();
}

From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDb.java

License:Open Source License

@Override
public String test() throws Exception {
    URI uri = new URI(getUrl());
    StringBuilder sb = new StringBuilder();
    if (!"mongodb".equals(uri.getScheme()))
        throw new SearchLibException(
                "Wrong scheme: " + uri.getScheme() + ". The URL should start with: mongodb://");
    MongoClient mongoClient = null;
    try {//from ww w.j a v a  2s  .  c o  m
        mongoClient = getMongoClient();
        sb.append("Connection established.");
        sb.append(StringUtils.LF);
        if (!StringUtils.isEmpty(databaseName)) {
            MongoDatabase db = mongoClient.getDatabase(databaseName);
            if (db == null)
                throw new SearchLibException("Database not found: " + databaseName);
            MongoIterable<String> collections = db.listCollectionNames();
            if (collections == null)
                throw new SearchLibException("No collection found.");
            sb.append("Collections found:");
            sb.append(StringUtils.LF);
            for (String collection : collections) {
                sb.append(collection);
                sb.append(StringUtils.LF);
            }
            if (!StringUtils.isEmpty(collectionName)) {
                MongoCollection<?> dbCollection = db.getCollection(collectionName);
                if (dbCollection == null)
                    throw new SearchLibException("Collection " + collectionName + " not found.");
                sb.append(
                        "Collection " + collectionName + " contains " + dbCollection.count() + " document(s).");
                sb.append(StringUtils.LF);
                if (!StringUtils.isEmpty(criteria)) {
                    long count = dbCollection.count(getCriteriaObject());
                    sb.append("Query returns " + count + " document(s).");
                    sb.append(StringUtils.LF);
                }
            }
        }
    } finally {
        if (mongoClient != null)
            mongoClient.close();
    }
    return sb.toString();
}

From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDbThread.java

License:Open Source License

@Override
public void runner() throws Exception {
    setStatus(CrawlStatus.STARTING);//from  ww w  .  ja  v  a2s  .c o  m

    MongoClient mongoClient = null;
    try {
        mongoClient = databaseCrawl.getMongoClient();
        MongoCollection<Document> collection = databaseCrawl.getCollection(mongoClient);
        FindIterable<Document> iterable = collection.find(databaseCrawl.getCriteriaObject());
        setStatus(CrawlStatus.CRAWL);
        if (iterable != null)
            runner_update(iterable);
        if (updatedIndexDocumentCount > 0 || updatedDeleteDocumentCount > 0)
            client.reload();
    } finally {
        if (mongoClient != null)
            mongoClient.close();
    }

}