/** * Attempts to find an existing MongoClient instance matching that URI in the holder, and returns it if exists. Otherwise creates a * new Mongo instance based on this URI and adds it to the holder. * * @param uri the Mongo URI * @return the client * @throws MongoException if there's a failure */ public Mongo connect(final MongoClientURI uri) { String key = toKey(uri); Mongo client = clients.get(key); if (client == null) { Mongo newbie = new MongoClient(uri); client = clients.putIfAbsent(key, newbie); if (client == null) { client = newbie; } else { newbie.close(); } } return client; }
/** * Attempts to find an existing MongoClient instance matching that URI in the holder, and returns it if exists. Otherwise creates a * new Mongo instance based on this URI and adds it to the holder. * * @param uri the Mongo URI * @return the client * @throws MongoException if there's a failure */ public Mongo connect(final MongoClientURI uri) { String key = toKey(uri); Mongo client = clients.get(key); if (client == null) { Mongo newbie = new MongoClient(uri); client = clients.putIfAbsent(key, newbie); if (client == null) { client = newbie; } else { newbie.close(); } } return client; }