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:org.aw20.mongoworkbench.MongoFactory.java

License:Open Source License

public void removeMongo(String sName) {
    MongoClient mclient = mongoMap.remove(sName);
    if (mclient != null)
        mclient.close();
}

From source file:org.basex.modules.MongoDB.java

License:BSD License

/**
 * take DB handler as parameter and get MongoClient and then close it.
 * @param handler DB handler//from  ww  w.  ja  va2s  .  c  om
 * @throws QueryException
 */
public void close(final Str handler) throws QueryException {
    String ch = handler.toJava();
    try {
        final MongoClient client = (MongoClient) getDbHandler(handler).getMongo();
        if (client == null)
            throw MongoDBErrors.mongoDBError(ch);
        client.close();
    } catch (MongoException e) {
        throw MongoDBErrors.mongoDBError(e);
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * take DB handler as parameter and get MongoClient and then close it.
 * @param handler database handler DB handler
 * @throws QueryException query exception
 */// www.  j av a  2 s.  com
public void disconnect(final Str handler) throws QueryException {
    String ch = handler.toJava();
    try {
        final MongoClient client = (MongoClient) getDbHandler(handler).getMongo();
        if (client == null)
            throw MongoDBErrors.mongoDBError(ch);
        client.close();
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(e);
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public void connect(ServerConfiguration configuration) {
    MongoClient mongo = null;
    try {/*from  w  ww  . java  2 s  . c o m*/
        String userDatabase = configuration.getUserDatabase();
        mongo = createMongoClient(configuration);

        com.mongodb.client.MongoDatabase databaseForTesting;
        if (StringUtils.isNotEmpty(userDatabase)) {
            databaseForTesting = mongo.getDatabase(userDatabase);
        } else {
            databaseForTesting = mongo.getDatabase("test");
        }
    } catch (IOException ex) {
        throw new MongoConnectionException(ex);
    } catch (MongoException ex) {
        LOG.error("Error when accessing Mongo server", ex);
        throw new MongoConnectionException(ex.getMessage());
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

List<MongoDatabase> loadDatabaseCollections(ServerConfiguration configuration) {
    MongoClient mongo = null;
    List<MongoDatabase> mongoDatabases = new LinkedList<MongoDatabase>();
    try {//from   w  w w  .j a  va  2s  . c om
        String userDatabase = configuration.getUserDatabase();

        mongo = createMongoClient(configuration);

        if (StringUtils.isNotEmpty(userDatabase) && configuration.isUserDatabaseAsMySingleDatabase()) {
            DB database = mongo.getDB(userDatabase);
            mongoDatabases.add(createMongoDatabaseAndItsCollections(database));
        } else {
            List<String> databaseNames = mongo.getDatabaseNames();
            Collections.sort(databaseNames);
            for (String databaseName : databaseNames) {
                DB database = mongo.getDB(databaseName);
                mongoDatabases.add(createMongoDatabaseAndItsCollections(database));
            }
        }

        return mongoDatabases;
    } catch (Exception ex) {
        LOG.error("Error when collecting Mongo databases", ex);
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public void update(ServerConfiguration configuration, MongoCollection mongoCollection, DBObject mongoDocument) {
    MongoClient mongo = null;
    try {//from  www  .ja  v  a  2 s .  co m
        String databaseName = mongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        DB database = mongo.getDB(databaseName);
        DBCollection collection = database.getCollection(mongoCollection.getName());

        collection.save(mongoDocument);
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public void delete(ServerConfiguration configuration, MongoCollection mongoCollection, Object _id) {
    MongoClient mongo = null;
    try {//  w  w  w  .  j av  a 2 s.c om
        String databaseName = mongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        DB database = mongo.getDB(databaseName);
        DBCollection collection = database.getCollection(mongoCollection.getName());

        collection.remove(new BasicDBObject("_id", _id));
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public void dropCollection(ServerConfiguration configuration, MongoCollection mongoCollection) {
    MongoClient mongo = null;
    try {//from  w w  w. ja  v a 2 s  .c  o  m
        String databaseName = mongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        DB database = mongo.getDB(databaseName);
        DBCollection collection = database.getCollection(mongoCollection.getName());

        collection.drop();
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public void dropDatabase(ServerConfiguration configuration, MongoDatabase selectedDatabase) {
    MongoClient mongo = null;
    try {//  w w  w  .  j a v  a2s .  c o m
        mongo = createMongoClient(configuration);
        mongo.dropDatabase(selectedDatabase.getName());
    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public MongoCollectionResult loadCollectionValues(ServerConfiguration configuration,
        MongoCollection mongoCollection, MongoQueryOptions mongoQueryOptions) {
    MongoClient mongo = null;
    try {/*from  www.ja  v a 2  s.c  o  m*/
        String databaseName = mongoCollection.getDatabaseName();
        mongo = createMongoClient(configuration);

        DB database = mongo.getDB(databaseName);
        DBCollection collection = database.getCollection(mongoCollection.getName());

        MongoCollectionResult mongoCollectionResult = new MongoCollectionResult(mongoCollection.getName());
        if (mongoQueryOptions.isAggregate()) {
            return aggregate(mongoQueryOptions, mongoCollectionResult, collection);
        }

        return find(mongoQueryOptions, mongoCollectionResult, collection);

    } catch (UnknownHostException ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}