Skip to content

Commit 6f91dc9

Browse files
committed
[JAVA-281]: when dropping a database or a collection, they remain in the driver cache, which can lead to problems
1 parent b1dd893 commit 6f91dc9

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

src/main/com/mongodb/DB.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public DB( Mongo mongo , String name ){
7171
*/
7272
public final DBCollection getCollection( String name ){
7373
DBCollection c = doGetCollection( name );
74-
if ( c != null ){
75-
_seenCollections.add( c );
76-
}
7774
return c;
7875
}
7976

@@ -327,15 +324,6 @@ public String toString(){
327324
return _name;
328325
}
329326

330-
/**
331-
* Clears any indices that have not yet been applied to
332-
* the collections in this database.
333-
*/
334-
public void resetIndexCache(){
335-
for ( DBCollection c : _seenCollections )
336-
c.resetIndexCache();
337-
}
338-
339327
/**
340328
* Gets the the error (if there is one) from the previous operation on this connection.
341329
* The result of this command will look like
@@ -414,7 +402,7 @@ public void dropDatabase()
414402

415403
CommandResult res = command(new BasicDBObject("dropDatabase", 1));
416404
res.throwOnError();
417-
405+
_mongo._dbs.remove(this.getName());
418406
}
419407

420408
/**
@@ -670,7 +658,6 @@ public int getOptions(){
670658

671659
final Mongo _mongo;
672660
final String _name;
673-
final Set<DBCollection> _seenCollections = Collections.synchronizedSet( new HashSet<DBCollection>() );
674661

675662
protected boolean _readOnly = false;
676663
private com.mongodb.WriteConcern _concern;

src/main/com/mongodb/DBApiLayer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ void killCursors( ServerAddress addr , List<Long> all )
170170
_connector.say( this , om ,com.mongodb.WriteConcern.NONE , addr );
171171
}
172172

173-
174173
class MyCollection extends DBCollection {
175174
MyCollection( String name ){
176175
super( DBApiLayer.this , name );
@@ -180,6 +179,12 @@ class MyCollection extends DBCollection {
180179
public void doapply( DBObject o ){
181180
}
182181

182+
@Override
183+
public void drop() throws MongoException {
184+
super.drop();
185+
_collections.remove(getName());
186+
}
187+
183188
public WriteResult insert(DBObject[] arr, com.mongodb.WriteConcern concern )
184189
throws MongoException {
185190
return insert( arr , true , concern );

src/main/com/mongodb/DBCollection.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,18 @@ public final void ensureIndex( final DBObject keys , final DBObject optionsIN )
449449

450450
final String name = options.get( "name" ).toString();
451451

452-
if ( _createIndexes.contains( name ) )
452+
if ( _createdIndexes.contains( name ) )
453453
return;
454454

455455
createIndex( keys , options );
456-
_createIndexes.add( name );
456+
_createdIndexes.add( name );
457457
}
458458

459459
/**
460460
* Clears all indices that have not yet been applied to this collection.
461461
*/
462462
public void resetIndexCache(){
463-
_createIndexes.clear();
463+
_createdIndexes.clear();
464464
}
465465

466466
DBObject defaultOptions( DBObject keys ){
@@ -675,12 +675,8 @@ public void dropIndexes( String name )
675675
.get();
676676

677677
CommandResult res = _db.command( cmd );
678-
if ( res.ok() || res.getErrorMessage().equals( "ns not found" ) ){
679-
resetIndexCache();
680-
return;
681-
}
682-
683-
throw new MongoException( "error dropping indexes : " + res );
678+
res.throwOnError();
679+
resetIndexCache();
684680
}
685681

686682
/**
@@ -689,11 +685,9 @@ public void dropIndexes( String name )
689685
*/
690686
public void drop()
691687
throws MongoException {
692-
resetIndexCache();
693688
CommandResult res =_db.command( BasicDBObjectBuilder.start().add( "drop" , getName() ).get() );
694-
if ( res.ok() || res.getErrorMessage().equals( "ns not found" ) )
695-
return;
696-
throw new MongoException( "error dropping : " + res );
689+
res.throwOnError();
690+
resetIndexCache();
697691
}
698692

699693
/**
@@ -1305,5 +1299,5 @@ public int getOptions(){
13051299
private Map<String,Class> _internalClass = Collections.synchronizedMap( new HashMap<String,Class>() );
13061300
private ReflectionDBObject.JavaWrapper _wrapper = null;
13071301

1308-
final private Set<String> _createIndexes = new HashSet<String>();
1302+
final private Set<String> _createdIndexes = new HashSet<String>();
13091303
}

0 commit comments

Comments
 (0)