@@ -90,6 +90,9 @@ public class Mongo {
9090 private static final String FULL_VERSION = "2.10.0-SNAPSHOT" ;
9191
9292 static int cleanerIntervalMS ;
93+
94+ private static final String ADMIN_DATABASE_NAME = "admin" ;
95+
9396 static {
9497 cleanerIntervalMS = Integer .parseInt (System .getProperty ("com.mongodb.cleanerIntervalMS" , "1000" ));
9598 }
@@ -328,8 +331,15 @@ public Mongo( MongoURI uri )
328331 _connector = new DBTCPConnector ( this , replicaSetSeeds );
329332 }
330333
331- if (uri .getDatabase () != null && uri .getUsername () != null ) {
332- DB db = new DBApiLayer (this , uri .getDatabase () , _connector , uri .getUsername (), uri .getPassword ());
334+ if (uri .getUsername () != null ) {
335+ String databaseName ;
336+ if (uri .getDatabase () != null ) {
337+ databaseName = uri .getDatabase ();
338+ } else {
339+ databaseName = ADMIN_DATABASE_NAME ;
340+ }
341+
342+ DB db = new DBApiLayer (this , databaseName , _connector , uri .getUsername (), uri .getPassword ());
333343 _dbs .put (db .getName (), db );
334344 }
335345
@@ -380,7 +390,7 @@ public List<String> getDatabaseNames(){
380390 cmd .put ("listDatabases" , 1 );
381391
382392
383- CommandResult res = getDB ( "admin" ).command (cmd , getOptions ());
393+ CommandResult res = getDB (ADMIN_DATABASE_NAME ).command (cmd , getOptions ());
384394 res .throwOnError ();
385395
386396 List l = (List )res .get ("databases" );
@@ -648,7 +658,7 @@ public CommandResult fsync(boolean async) {
648658 if (async ) {
649659 cmd .put ("async" , 1 );
650660 }
651- return getDB ("admin" ).command (cmd );
661+ return getDB (ADMIN_DATABASE_NAME ).command (cmd );
652662 }
653663
654664 /**
@@ -660,7 +670,7 @@ public CommandResult fsync(boolean async) {
660670 public CommandResult fsyncAndLock () {
661671 DBObject cmd = new BasicDBObject ("fsync" , 1 );
662672 cmd .put ("lock" , 1 );
663- return getDB ("admin" ).command (cmd );
673+ return getDB (ADMIN_DATABASE_NAME ).command (cmd );
664674 }
665675
666676 /**
@@ -670,7 +680,7 @@ public CommandResult fsyncAndLock() {
670680 * @throws MongoException
671681 */
672682 public DBObject unlock () {
673- DB db = getDB ("admin" );
683+ DB db = getDB (ADMIN_DATABASE_NAME );
674684 DBCollection col = db .getCollection ("$cmd.sys.unlock" );
675685 return col .findOne ();
676686 }
@@ -681,7 +691,7 @@ public DBObject unlock() {
681691 * @throws MongoException
682692 */
683693 public boolean isLocked () {
684- DB db = getDB ("admin" );
694+ DB db = getDB (ADMIN_DATABASE_NAME );
685695 DBCollection col = db .getCollection ("$cmd.sys.inprog" );
686696 BasicDBObject res = (BasicDBObject ) col .findOne ();
687697 if (res .containsField ("fsyncLock" )) {
0 commit comments