Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Simplify Mongo connection URL parsing by utilizing MongoURI #1

Merged
merged 1 commit into from Nov 28, 2011

Conversation

Projects
None yet
3 participants

I've simplified the connection URL parsing by utilizing com.mongodb.MongoURI instead of pattern matching. Since MongoURI is provided out of the box by the Mongo driver this cuts down considerably on code. Also made testing locally with anonymous a possibility.

@jamesward jamesward added a commit that referenced this pull request Nov 28, 2011

@jamesward jamesward Merge pull request #1 from dashorst/patch-1
Simplify Mongo connection URL parsing by utilizing MongoURI
da53ed7

@jamesward jamesward merged commit da53ed7 into jamesward:master Nov 28, 2011

Owner

jamesward commented Nov 28, 2011

Thanks Martijn! That is so much simpler.

Hi is there someway to get MongoClient from the connection url like following? I need MongoClient object so that I can close it after complete the query.
MongoClient mongoClient = (MongoClient)mongoDbURI.connect();
above code get error.
thank you

ok I got solution. might be helpful for others

MongoClientURI uri = new MongoClientURI(url.toJava());
String handler = "Client" + connections.size();
try {
MongoClient mongoClient = new MongoClient(uri);
connections.put(handler, mongoClient);
//select database
final String dbh = "DB" + dbs.size();
try {
DB db = mongoClient.getDB(uri.getDatabase());
dbs.put(dbh, db);
return Str.get(dbh);

        } catch (final MongoException ex) {
            throw new QueryException(ex);
        }

    } catch (final MongoException ex) {
        throw new QueryException(ex);
    } catch (UnknownHostException ex) {
        throw new QueryException(ex);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment