Skip to content

Commit f7bb1d7

Browse files
committed
In the connection pool, moved wait queue size increment into its own try block so that wait queue start and end events are only delivered if the wait queue is actually entered.
JAVA-1873
1 parent 93fb850 commit f7bb1d7

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

driver-core/src/main/com/mongodb/connection/DefaultConnectionPool.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,31 @@ public InternalConnection get() {
8383
@Override
8484
public InternalConnection get(final long timeout, final TimeUnit timeUnit) {
8585
try {
86-
connectionPoolListener.waitQueueEntered(new ConnectionPoolWaitQueueEvent(serverId, currentThread().getId()));
8786
if (waitQueueSize.incrementAndGet() > settings.getMaxWaitQueueSize()) {
8887
throw createWaitQueueFullException();
8988
}
90-
PooledConnection pooledConnection = getPooledConnection(timeout, timeUnit);
91-
if (!pooledConnection.opened()) {
92-
try {
93-
pooledConnection.open();
94-
} catch (Throwable t) {
95-
pool.release(pooledConnection.wrapped, true);
96-
if (t instanceof MongoException) {
97-
throw (MongoException) t;
98-
} else {
99-
throw new MongoInternalException(t.toString(), t);
89+
try {
90+
connectionPoolListener.waitQueueEntered(new ConnectionPoolWaitQueueEvent(serverId, currentThread().getId()));
91+
PooledConnection pooledConnection = getPooledConnection(timeout, timeUnit);
92+
if (!pooledConnection.opened()) {
93+
try {
94+
pooledConnection.open();
95+
} catch (Throwable t) {
96+
pool.release(pooledConnection.wrapped, true);
97+
if (t instanceof MongoException) {
98+
throw (MongoException) t;
99+
} else {
100+
throw new MongoInternalException(t.toString(), t);
101+
}
100102
}
101103
}
102-
}
103104

104-
return pooledConnection;
105+
return pooledConnection;
106+
} finally {
107+
connectionPoolListener.waitQueueExited(new ConnectionPoolWaitQueueEvent(serverId, currentThread().getId()));
108+
}
105109
} finally {
106110
waitQueueSize.decrementAndGet();
107-
connectionPoolListener.waitQueueExited(new ConnectionPoolWaitQueueEvent(serverId, currentThread().getId()));
108111
}
109112
}
110113

driver-core/src/test/functional/com/mongodb/connection/DefaultConnectionPoolTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2626
import static java.util.concurrent.TimeUnit.MINUTES;
27+
import static java.util.concurrent.TimeUnit.SECONDS;
2728
import static junit.framework.TestCase.assertNotNull;
2829
import static org.junit.Assert.assertEquals;
2930
import static org.junit.Assert.assertTrue;
@@ -237,14 +238,15 @@ public void shouldNotCallWaitQueueExitedIfWaitQueueEnteredWasNotCalled() throws
237238
ConnectionPoolSettings.builder()
238239
.maxSize(1)
239240
.maxWaitQueueSize(1)
240-
.maxWaitTime(500, MILLISECONDS)
241+
.maxWaitTime(5, SECONDS)
241242
.build(),
242243
listener);
243244

244245
// when
245-
provider.get();
246+
InternalConnection connection = provider.get();
246247

247-
new Thread(new TimeoutTrackingConnectionGetter(provider)).start();
248+
TimeoutTrackingConnectionGetter timeoutTrackingConnectionGetter = new TimeoutTrackingConnectionGetter(provider);
249+
new Thread(timeoutTrackingConnectionGetter).start();
248250
Thread.sleep(100);
249251

250252
try {
@@ -254,7 +256,15 @@ public void shouldNotCallWaitQueueExitedIfWaitQueueEnteredWasNotCalled() throws
254256
// all good
255257
}
256258

259+
// when
260+
connection.close();
261+
262+
timeoutTrackingConnectionGetter.getLatch().await();
263+
257264
// then
258-
assertEquals(1, listener.getWaitQueueSize());
265+
connection = provider.get();
266+
267+
// cleanup
268+
connection.close();
259269
}
260270
}

driver-core/src/test/functional/com/mongodb/connection/QueueEventsConnectionPoolListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class QueueEventsConnectionPoolListener extends ConnectionPoolListenerAdapter {
2525
private final AtomicInteger waitQueueSize = new AtomicInteger();
2626

2727
@Override
28-
public void waitQueueExited(ConnectionPoolWaitQueueEvent event) {
28+
public void waitQueueExited(final ConnectionPoolWaitQueueEvent event) {
2929
waitQueueSize.decrementAndGet();
3030
}
3131

3232
@Override
33-
public void waitQueueEntered(ConnectionPoolWaitQueueEvent event) {
33+
public void waitQueueEntered(final ConnectionPoolWaitQueueEvent event) {
3434
waitQueueSize.incrementAndGet();
3535
}
3636

driver-core/src/test/unit/com/mongodb/connection/TimeoutTrackingConnectionGetter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ boolean isGotTimeout() {
3737
@Override
3838
public void run() {
3939
try {
40-
connectionPool.get();
40+
InternalConnection connection = connectionPool.get();
41+
connection.close();
4142
} catch (MongoTimeoutException e) {
4243
gotTimeout = true;
4344
} finally {

0 commit comments

Comments
 (0)