public <T> T execute(String collectionName, CollectionCallback<T> callback) { MongoDatabase database = null; if (mongoClient == null) { mongoClient = getConnectedClient(); } return callback.doInCollection( mongoClient.getDatabase(databaseName).getCollection(collectionName)); }
public <T> T execute(String collectionName, CollectionCallback<T> callback) { MongoDatabase database = null; if (mongoClient == null) { mongoClient = getConnectedClient(); } return callback.doInCollection( mongoClient.getDatabase(databaseName).getCollection(collectionName)); }
/** * Connect MongoDB and call callback, close connection at last. * * @param collectionName Collection name. * @param callback Callback * @param <T> The type of value which you want to return. * @return The value which callback returned. */ public <T> T connect(String collectionName, CollectionCallback<T> callback) { MongoClient client = null; T t = null; try { client = getConnectedClient(); MongoDatabase database = client.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(collectionName); t = callback.doInCollection(collection); } finally { if (client != null) { client.close(); } } return t; }
/** * Connect MongoDB and call callback, close connection at last. * * @param collectionName Collection name. * @param callback Callback * @param <T> The type of value which you want to return. * @return The value which callback returned. */ public <T> T connect(String collectionName, CollectionCallback<T> callback) { MongoClient client = null; T t = null; try { client = getConnectedClient(); MongoDatabase database = client.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(collectionName); t = callback.doInCollection(collection); } finally { if (client != null) { client.close(); } } return t; }