@@ -90,6 +90,9 @@ public class Mongo {
90
90
private static final String FULL_VERSION = "2.10.0-SNAPSHOT" ;
91
91
92
92
static int cleanerIntervalMS ;
93
+
94
+ private static final String ADMIN_DATABASE_NAME = "admin" ;
95
+
93
96
static {
94
97
cleanerIntervalMS = Integer .parseInt (System .getProperty ("com.mongodb.cleanerIntervalMS" , "1000" ));
95
98
}
@@ -328,8 +331,15 @@ public Mongo( MongoURI uri )
328
331
_connector = new DBTCPConnector ( this , replicaSetSeeds );
329
332
}
330
333
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 ());
333
343
_dbs .put (db .getName (), db );
334
344
}
335
345
@@ -380,7 +390,7 @@ public List<String> getDatabaseNames(){
380
390
cmd .put ("listDatabases" , 1 );
381
391
382
392
383
- CommandResult res = getDB ( "admin" ).command (cmd , getOptions ());
393
+ CommandResult res = getDB (ADMIN_DATABASE_NAME ).command (cmd , getOptions ());
384
394
res .throwOnError ();
385
395
386
396
List l = (List )res .get ("databases" );
@@ -648,7 +658,7 @@ public CommandResult fsync(boolean async) {
648
658
if (async ) {
649
659
cmd .put ("async" , 1 );
650
660
}
651
- return getDB ("admin" ).command (cmd );
661
+ return getDB (ADMIN_DATABASE_NAME ).command (cmd );
652
662
}
653
663
654
664
/**
@@ -660,7 +670,7 @@ public CommandResult fsync(boolean async) {
660
670
public CommandResult fsyncAndLock () {
661
671
DBObject cmd = new BasicDBObject ("fsync" , 1 );
662
672
cmd .put ("lock" , 1 );
663
- return getDB ("admin" ).command (cmd );
673
+ return getDB (ADMIN_DATABASE_NAME ).command (cmd );
664
674
}
665
675
666
676
/**
@@ -670,7 +680,7 @@ public CommandResult fsyncAndLock() {
670
680
* @throws MongoException
671
681
*/
672
682
public DBObject unlock () {
673
- DB db = getDB ("admin" );
683
+ DB db = getDB (ADMIN_DATABASE_NAME );
674
684
DBCollection col = db .getCollection ("$cmd.sys.unlock" );
675
685
return col .findOne ();
676
686
}
@@ -681,7 +691,7 @@ public DBObject unlock() {
681
691
* @throws MongoException
682
692
*/
683
693
public boolean isLocked () {
684
- DB db = getDB ("admin" );
694
+ DB db = getDB (ADMIN_DATABASE_NAME );
685
695
DBCollection col = db .getCollection ("$cmd.sys.inprog" );
686
696
BasicDBObject res = (BasicDBObject ) col .findOne ();
687
697
if (res .containsField ("fsyncLock" )) {
0 commit comments