28
28
/**
29
29
* A database connection with internal pooling.
30
30
* For most application, you should have 1 Mongo instance for the entire JVM.
31
- *
31
+ *
32
32
* The following are equivalent, and all connect to the
33
33
* local database running on the default port:
34
34
*
45
45
*
46
46
* <h3>Connecting to a Replica Pair</h3>
47
47
* <p>
48
- * You can connect to a
48
+ * You can connect to a
49
49
* <a href="http://www.mongodb.org/display/DOCS/Replica+Pairs">replica pair</a>
50
50
* using the Java driver by passing two DBAddresses to the Mongo constructor.
51
51
* For example:
52
52
* </p>
53
53
* <blockquote><pre>
54
- * DBAddress left = new DBAddress("localhost :27017/test");
55
- * DBAddress right = new DBAddress("localhost :27018/test");
54
+ * DBAddress left = new DBAddress("127.0.0.1 :27017/test");
55
+ * DBAddress right = new DBAddress("127.0.0.1 :27018/test");
56
56
*
57
57
* Mongo mongo = new Mongo(left, right);
58
58
* </pre></blockquote>
59
- *
59
+ *
60
60
* <p>
61
61
* If the master of a replica pair goes down, there will be a brief lag before
62
62
* the slave becomes master. Thus, your application should be prepared to catch
71
71
*
72
72
* <h3>Connecting to a Replica Set</h3>
73
73
* <p>
74
- * You can connect to a
74
+ * You can connect to a
75
75
* <a href="http://www.mongodb.org/display/DOCS/Replica+Sets">replica set</a>
76
76
* using the Java driver by passing several a list if ServerAddress to the
77
77
* Mongo constructor.
78
78
* For example:
79
79
* </p>
80
80
* <blockquote><pre>
81
81
* List<ServerAddress> addrs = new ArrayList<ServerAddress>();
82
- * addrs.add( new ServerAddress( "localhost " , 27017 ) );
83
- * addrs.add( new ServerAddress( "localhost " , 27018 ) );
84
- * addrs.add( new ServerAddress( "localhost " , 27019 ) );
82
+ * addrs.add( new ServerAddress( "127.0.0.1 " , 27017 ) );
83
+ * addrs.add( new ServerAddress( "127.0.0.1 " , 27018 ) );
84
+ * addrs.add( new ServerAddress( "127.0.0.1 " , 27019 ) );
85
85
*
86
86
* Mongo mongo = new Mongo( addrs );
87
87
* </pre></blockquote>
@@ -198,7 +198,7 @@ public Mongo( ServerAddress addr , MongoOptions options )
198
198
* <p>Creates a Mongo in paired mode. <br/> This will also work for
199
199
* a replica set and will find all members (the master will be used by
200
200
* default).</p>
201
- *
201
+ *
202
202
* @see com.mongodb.ServerAddress
203
203
* @param left left side of the pair
204
204
* @param right right side of the pair
@@ -213,7 +213,7 @@ public Mongo( ServerAddress left , ServerAddress right )
213
213
* <p>Creates a Mongo connection in paired mode. <br/> This will also work for
214
214
* a replica set and will find all members (the master will be used by
215
215
* default).</p>
216
- *
216
+ *
217
217
* @see com.mongodb.ServerAddress
218
218
* @param left left side of the pair
219
219
* @param right right side of the pair
@@ -249,14 +249,14 @@ public Mongo( List<ServerAddress> replicaSetSeeds )
249
249
* <p>Creates a Mongo based on a replica set, or pair.
250
250
* It will find all members (the master will be used by default).</p>
251
251
* @see com.mongodb.ServerAddress
252
- * @param replicaSetSeeds put as many servers as you can in the list.
252
+ * @param replicaSetSeeds put as many servers as you can in the list.
253
253
* the system will figure the rest out
254
254
* @param options default query options
255
255
* @throws MongoException
256
- */
256
+ */
257
257
public Mongo ( List <ServerAddress > replicaSetSeeds , MongoOptions options )
258
258
throws MongoException {
259
-
259
+
260
260
_addr = null ;
261
261
_addrs = replicaSetSeeds ;
262
262
_options = options ;
@@ -273,20 +273,20 @@ public Mongo( List<ServerAddress> replicaSetSeeds , MongoOptions options )
273
273
* @param uri
274
274
* @see MongoURI
275
275
* <p>examples:
276
- * <li>mongodb://localhost </li>
277
- * <li>mongodb://fred:foobar@localhost /</li>
276
+ * <li>mongodb://127.0.0.1 </li>
277
+ * <li>mongodb://fred:foobar@127.0.0.1 /</li>
278
278
* </p>
279
279
* @throws MongoException
280
280
* @throws UnknownHostException
281
281
* @dochub connections
282
- */
282
+ */
283
283
284
284
public Mongo ( MongoURI uri )
285
285
throws MongoException , UnknownHostException {
286
286
287
287
_options = uri .getOptions ();
288
288
_applyMongoOptions ();
289
-
289
+
290
290
if ( uri .getHosts ().size () == 1 ){
291
291
_addr = new ServerAddress ( uri .getHosts ().get (0 ) );
292
292
_addrs = null ;
@@ -311,11 +311,11 @@ public Mongo( MongoURI uri )
311
311
* @return
312
312
*/
313
313
public DB getDB ( String dbname ){
314
-
314
+
315
315
DB db = _dbs .get ( dbname );
316
316
if ( db != null )
317
317
return db ;
318
-
318
+
319
319
db = new DBApiLayer ( this , dbname , _connector );
320
320
DB temp = _dbs .putIfAbsent ( dbname , db );
321
321
if ( temp != null )
@@ -342,7 +342,7 @@ public List<String> getDatabaseNames()
342
342
343
343
BasicDBObject cmd = new BasicDBObject ();
344
344
cmd .put ("listDatabases" , 1 );
345
-
345
+
346
346
347
347
BasicDBObject res = (BasicDBObject )getDB ( "admin" ).command (cmd , getOptions ());
348
348
@@ -367,7 +367,7 @@ public List<String> getDatabaseNames()
367
367
*/
368
368
public void dropDatabase (String dbName )
369
369
throws MongoException {
370
-
370
+
371
371
getDB ( dbName ).dropDatabase ();
372
372
}
373
373
@@ -480,7 +480,7 @@ public void setOptions( int options ){
480
480
public void resetOptions (){
481
481
_netOptions .reset ();
482
482
}
483
-
483
+
484
484
/**
485
485
* gets the default query options
486
486
* @return
@@ -510,26 +510,26 @@ DBTCPConnector getConnector() {
510
510
private WriteConcern _concern = WriteConcern .NORMAL ;
511
511
final Bytes .OptionHolder _netOptions = new Bytes .OptionHolder ( null );
512
512
final DBCleanerThread _cleaner ;
513
-
514
- org .bson .util .SimplePool <PoolOutputBuffer > _bufferPool =
513
+
514
+ org .bson .util .SimplePool <PoolOutputBuffer > _bufferPool =
515
515
new org .bson .util .SimplePool <PoolOutputBuffer >( 1000 ){
516
-
516
+
517
517
protected PoolOutputBuffer createNew (){
518
518
return new PoolOutputBuffer ();
519
519
}
520
-
520
+
521
521
};
522
522
523
523
524
- // -------
524
+ // -------
525
+
525
526
526
-
527
527
/**
528
528
* Mongo.Holder can be used as a static place to hold several instances of Mongo.
529
529
* Security is not enforced at this level, and needs to be done on the application side.
530
530
*/
531
531
public static class Holder {
532
-
532
+
533
533
/**
534
534
* Attempts to find an existing Mongo instance matching that URI in the holder, and returns it if exists.
535
535
* Otherwise creates a new Mongo instance based on this URI and adds it to the holder.
@@ -542,19 +542,19 @@ public Mongo connect( MongoURI uri )
542
542
throws MongoException , UnknownHostException {
543
543
544
544
String key = _toKey ( uri );
545
-
545
+
546
546
Mongo m = _mongos .get (key );
547
547
if ( m != null )
548
548
return m ;
549
549
550
550
m = new Mongo ( uri );
551
-
551
+
552
552
Mongo temp = _mongos .putIfAbsent ( key , m );
553
553
if ( temp == null ){
554
554
// ours got in
555
555
return m ;
556
556
}
557
-
557
+
558
558
// there was a race and we lost
559
559
// close ours and return the other one
560
560
m .close ();
@@ -570,9 +570,9 @@ String _toKey( MongoURI uri ){
570
570
return buf .toString ();
571
571
}
572
572
573
-
573
+
574
574
private static final ConcurrentMap <String ,Mongo > _mongos = new ConcurrentHashMap <String ,Mongo >();
575
-
575
+
576
576
}
577
577
578
578
class DBCleanerThread extends Thread {
0 commit comments