Skip to content

Commit 93fb850

Browse files
fzhinkinjyemin
authored andcommitted
Notify listener about wait queue enter before throwing an exception if the queue is full.
JAVA-1873
1 parent 44200b1 commit 93fb850

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright (c) 2008-2015 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,10 +83,10 @@ 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()));
8687
if (waitQueueSize.incrementAndGet() > settings.getMaxWaitQueueSize()) {
8788
throw createWaitQueueFullException();
8889
}
89-
connectionPoolListener.waitQueueEntered(new ConnectionPoolWaitQueueEvent(serverId, currentThread().getId()));
9090
PooledConnection pooledConnection = getPooledConnection(timeout, timeUnit);
9191
if (!pooledConnection.opened()) {
9292
try {

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright (c) 2008-2015 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -227,4 +227,34 @@ public void shouldPruneAfterMaintenanceTaskRuns() throws InterruptedException {
227227
// then
228228
assertTrue(connectionFactory.getCreatedConnections().get(0).isClosed());
229229
}
230+
231+
@Test
232+
public void shouldNotCallWaitQueueExitedIfWaitQueueEnteredWasNotCalled() throws InterruptedException {
233+
// given
234+
QueueEventsConnectionPoolListener listener = new QueueEventsConnectionPoolListener();
235+
236+
provider = new DefaultConnectionPool(SERVER_ID, connectionFactory,
237+
ConnectionPoolSettings.builder()
238+
.maxSize(1)
239+
.maxWaitQueueSize(1)
240+
.maxWaitTime(500, MILLISECONDS)
241+
.build(),
242+
listener);
243+
244+
// when
245+
provider.get();
246+
247+
new Thread(new TimeoutTrackingConnectionGetter(provider)).start();
248+
Thread.sleep(100);
249+
250+
try {
251+
provider.get();
252+
fail();
253+
} catch (MongoWaitQueueFullException e) {
254+
// all good
255+
}
256+
257+
// then
258+
assertEquals(1, listener.getWaitQueueSize());
259+
}
230260
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2008-2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.connection;
18+
19+
import com.mongodb.event.ConnectionPoolListenerAdapter;
20+
import com.mongodb.event.ConnectionPoolWaitQueueEvent;
21+
22+
import java.util.concurrent.atomic.AtomicInteger;
23+
24+
class QueueEventsConnectionPoolListener extends ConnectionPoolListenerAdapter {
25+
private final AtomicInteger waitQueueSize = new AtomicInteger();
26+
27+
@Override
28+
public void waitQueueExited(ConnectionPoolWaitQueueEvent event) {
29+
waitQueueSize.decrementAndGet();
30+
}
31+
32+
@Override
33+
public void waitQueueEntered(ConnectionPoolWaitQueueEvent event) {
34+
waitQueueSize.incrementAndGet();
35+
}
36+
37+
public int getWaitQueueSize() {
38+
return waitQueueSize.get();
39+
}
40+
}

0 commit comments

Comments
 (0)