Skip to content

Commit 57c5774

Browse files
committed
better handling of Mongo.close
1 parent 6dcd6a6 commit 57c5774

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public void requestEnsureConnection(){
112112
_myPort.get().requestEnsureConnection();
113113
}
114114

115+
void _checkClosed(){
116+
if ( _closed )
117+
throw new IllegalStateException( "this Mongo has been closed" );
118+
}
119+
115120
WriteResult _checkWriteError( DB db , MyPort mp , DBPort port , WriteConcern concern )
116121
throws MongoException {
117122

@@ -135,6 +140,9 @@ WriteResult _checkWriteError( DB db , MyPort mp , DBPort port , WriteConcern con
135140

136141
public WriteResult say( DB db , OutMessage m , WriteConcern concern )
137142
throws MongoException {
143+
144+
_checkClosed();
145+
138146
MyPort mp = _myPort.get();
139147
DBPort port = mp.get( true , false );
140148
port.checkAuth( db );
@@ -181,6 +189,8 @@ public Response call( DB db , DBCollection coll , OutMessage m )
181189
public Response call( DB db , DBCollection coll , OutMessage m , int retries )
182190
throws MongoException {
183191

192+
_checkClosed();
193+
184194
final MyPort mp = _myPort.get();
185195
final DBPort port = mp.get( false , m.hasOption( Bytes.QUERYOPTION_SLAVEOK ) );
186196

@@ -337,6 +347,7 @@ public String debugString(){
337347
}
338348

339349
public void close(){
350+
_closed = true;
340351
_portHolder.close();
341352
_rsStatus.close();
342353
}
@@ -347,6 +358,7 @@ public void close(){
347358
private DBPortPool.Holder _portHolder;
348359
private final List<ServerAddress> _allHosts;
349360
private final ReplicaSetStatus _rsStatus;
361+
private boolean _closed = false;
350362

351363
private final ThreadLocal<MyPort> _myPort = new ThreadLocal<MyPort>(){
352364
protected MyPort initialValue(){

src/main/com/mongodb/Mongo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public List<ServerAddress> getAllAddress() {
291291

292292
/**
293293
* closes all open connections
294-
* this Mongo instance can be re-used however
294+
* this Mongo cannot be re-used
295295
*/
296296
public void close(){
297297
_connector.close();

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ boolean ready(){
3838
return _setName != null;
3939
}
4040

41+
void _checkClosed(){
42+
if ( _closed )
43+
throw new IllegalStateException( "ReplicaSetStatus closed" );
44+
}
45+
4146
/**
4247
* @return master or null if don't have one
4348
*/
@@ -49,6 +54,7 @@ ServerAddress getMaster(){
4954
}
5055

5156
Node getMasterNode(){
57+
_checkClosed();
5258
for ( int i=0; i<_all.size(); i++ ){
5359
Node n = _all.get(i);
5460
if ( n.master() )
@@ -62,6 +68,7 @@ Node getMasterNode(){
6268
* @return a good secondary or null if can't find one
6369
*/
6470
ServerAddress getASecondary(){
71+
_checkClosed();
6572
Node best = null;
6673

6774
int start = _random.nextInt( _all.size() );
@@ -199,7 +206,7 @@ class Updater extends Thread {
199206
}
200207

201208
public void run(){
202-
while ( true ){
209+
while ( ! _closed ){
203210
try {
204211
updateAll();
205212
}
@@ -286,7 +293,7 @@ void printStatus(){
286293
}
287294

288295
void close(){
289-
// TODO
296+
_closed = false;
290297
}
291298

292299
final Mongo _mongo;
@@ -298,7 +305,8 @@ void close(){
298305
Logger _logger = _rootLogger; // will get changed to use set name once its found
299306

300307
String _lastPrimarySignal;
301-
308+
boolean _closed = false;
309+
302310
final Random _random = new Random();
303311

304312
static final MongoOptions _mongoOptions = new MongoOptions();

0 commit comments

Comments
 (0)