@@ -105,6 +105,11 @@ public class Mongo {
105105 */
106106 public static final int MINOR_VERSION = 4 ;
107107
108+ static int cleanerIntervalMS ;
109+ static {
110+ cleanerIntervalMS = Integer .parseInt (System .getProperty ("com.mongodb.cleanerIntervalMS" , "1000" ));
111+ }
112+
108113 /**
109114 * returns a database object
110115 * @param addr the database address
@@ -185,6 +190,8 @@ public Mongo( ServerAddress addr , MongoOptions options )
185190 _options = options ;
186191 _applyMongoOptions ();
187192 _connector = new DBTCPConnector ( this , _addr );
193+ _cleaner = new DBCleanerThread ();
194+ _cleaner .start ();
188195 }
189196
190197 /**
@@ -220,6 +227,9 @@ public Mongo( ServerAddress left , ServerAddress right , MongoOptions options )
220227 _options = options ;
221228 _applyMongoOptions ();
222229 _connector = new DBTCPConnector ( this , _addrs );
230+
231+ _cleaner = new DBCleanerThread ();
232+ _cleaner .start ();
223233 }
224234
225235 /**
@@ -252,6 +262,9 @@ public Mongo( List<ServerAddress> replicaSetSeeds , MongoOptions options )
252262 _options = options ;
253263 _applyMongoOptions ();
254264 _connector = new DBTCPConnector ( this , _addrs );
265+
266+ _cleaner = new DBCleanerThread ();
267+ _cleaner .start ();
255268 }
256269
257270 /**
@@ -288,6 +301,8 @@ public Mongo( MongoURI uri )
288301 _connector = new DBTCPConnector ( this , replicaSetSeeds );
289302 }
290303
304+ _cleaner = new DBCleanerThread ();
305+ _cleaner .start ();
291306 }
292307
293308 /**
@@ -494,6 +509,7 @@ DBTCPConnector getConnector() {
494509 final ConcurrentMap <String ,DB > _dbs = new ConcurrentHashMap <String ,DB >();
495510 private WriteConcern _concern = WriteConcern .NORMAL ;
496511 final Bytes .OptionHolder _netOptions = new Bytes .OptionHolder ( null );
512+ final DBCleanerThread _cleaner ;
497513
498514 org .bson .util .SimplePool <PoolOutputBuffer > _bufferPool =
499515 new org .bson .util .SimplePool <PoolOutputBuffer >( 1000 ){
@@ -559,4 +575,24 @@ String _toKey( MongoURI uri ){
559575
560576 }
561577
578+ class DBCleanerThread extends Thread {
579+
580+ DBCleanerThread () {
581+ setDaemon (true );
582+ setName ("MongoCleaner" + hashCode ());
583+ }
584+
585+ public void run () {
586+ while (_connector .isOpen ()) {
587+ try {
588+ Thread .sleep (cleanerIntervalMS );
589+ for (DB db : _dbs .values ()) {
590+ db .cleanCursors (true );
591+ }
592+ } catch (Throwable t ) {
593+ // thread must never die
594+ }
595+ }
596+ }
597+ }
562598}
0 commit comments