44
44
import java .util .concurrent .Executors ;
45
45
import java .util .concurrent .ScheduledExecutorService ;
46
46
import java .util .concurrent .TimeUnit ;
47
+ import java .util .concurrent .atomic .AtomicBoolean ;
47
48
import java .util .concurrent .atomic .AtomicInteger ;
48
49
49
50
import static com .mongodb .assertions .Assertions .isTrue ;
@@ -372,47 +373,48 @@ private ConnectionId getId(final InternalConnection internalConnection) {
372
373
}
373
374
374
375
private class PooledConnection implements InternalConnection {
375
- private volatile UsageTrackingInternalConnection wrapped ;
376
+ private final UsageTrackingInternalConnection wrapped ;
377
+ private final AtomicBoolean isClosed = new AtomicBoolean ();
376
378
377
379
public PooledConnection (final UsageTrackingInternalConnection wrapped ) {
378
380
this .wrapped = notNull ("wrapped" , wrapped );
379
381
}
380
382
381
383
@ Override
382
384
public void open () {
383
- isTrue ("open" , wrapped != null );
385
+ isTrue ("open" , ! isClosed . get () );
384
386
wrapped .open ();
385
387
}
386
388
387
389
@ Override
388
390
public void openAsync (final SingleResultCallback <Void > callback ) {
389
- isTrue ("open" , wrapped != null );
391
+ isTrue ("open" , ! isClosed . get () );
390
392
wrapped .openAsync (callback );
391
393
}
392
394
393
395
@ Override
394
396
public void close () {
395
- if (wrapped != null ) {
396
- if (!closed ) {
397
+ // All but the first call is a no-op
398
+ if (!isClosed .getAndSet (true )) {
399
+ if (!DefaultConnectionPool .this .closed ) {
397
400
connectionPoolListener .connectionCheckedIn (new ConnectionCheckedInEvent (getId (wrapped )));
398
401
if (LOGGER .isTraceEnabled ()) {
399
402
LOGGER .trace (format ("Checked in connection [%s] to server %s" , getId (wrapped ), serverId .getAddress ()));
400
403
}
401
404
}
402
405
pool .release (wrapped , wrapped .isClosed () || shouldPrune (wrapped ));
403
- wrapped = null ;
404
406
}
405
407
}
406
408
407
409
@ Override
408
410
public boolean opened () {
409
- isTrue ("open" , wrapped != null );
411
+ isTrue ("open" , ! isClosed . get () );
410
412
return wrapped .opened ();
411
413
}
412
414
413
415
@ Override
414
416
public boolean isClosed () {
415
- return wrapped == null || wrapped .isClosed ();
417
+ return isClosed . get () || wrapped .isClosed ();
416
418
}
417
419
418
420
@ Override
@@ -422,7 +424,7 @@ public ByteBuf getBuffer(final int capacity) {
422
424
423
425
@ Override
424
426
public void sendMessage (final List <ByteBuf > byteBuffers , final int lastRequestId ) {
425
- isTrue ("open" , wrapped != null );
427
+ isTrue ("open" , ! isClosed . get () );
426
428
try {
427
429
wrapped .sendMessage (byteBuffers , lastRequestId );
428
430
} catch (MongoException e ) {
@@ -433,7 +435,7 @@ public void sendMessage(final List<ByteBuf> byteBuffers, final int lastRequestId
433
435
434
436
@ Override
435
437
public ResponseBuffers receiveMessage (final int responseTo ) {
436
- isTrue ("open" , wrapped != null );
438
+ isTrue ("open" , ! isClosed . get () );
437
439
try {
438
440
return wrapped .receiveMessage (responseTo );
439
441
} catch (MongoException e ) {
@@ -444,7 +446,7 @@ public ResponseBuffers receiveMessage(final int responseTo) {
444
446
445
447
@ Override
446
448
public void sendMessageAsync (final List <ByteBuf > byteBuffers , final int lastRequestId , final SingleResultCallback <Void > callback ) {
447
- isTrue ("open" , wrapped != null );
449
+ isTrue ("open" , ! isClosed . get () );
448
450
wrapped .sendMessageAsync (byteBuffers , lastRequestId , new SingleResultCallback <Void >() {
449
451
@ Override
450
452
public void onResult (final Void result , final Throwable t ) {
@@ -458,7 +460,7 @@ public void onResult(final Void result, final Throwable t) {
458
460
459
461
@ Override
460
462
public void receiveMessageAsync (final int responseTo , final SingleResultCallback <ResponseBuffers > callback ) {
461
- isTrue ("open" , wrapped != null );
463
+ isTrue ("open" , ! isClosed . get () );
462
464
wrapped .receiveMessageAsync (responseTo , new SingleResultCallback <ResponseBuffers >() {
463
465
@ Override
464
466
public void onResult (final ResponseBuffers result , final Throwable t ) {
@@ -472,7 +474,7 @@ public void onResult(final ResponseBuffers result, final Throwable t) {
472
474
473
475
@ Override
474
476
public ConnectionDescription getDescription () {
475
- isTrue ("open" , wrapped != null );
477
+ isTrue ("open" , ! isClosed . get () );
476
478
return wrapped .getDescription ();
477
479
}
478
480
}
0 commit comments