Skip to content

Commit 5f2c539

Browse files
authored
Improve MongoTimeoutException thrown by DefaultConnectionPool (#1767)
JAVA-5910
1 parent 0a110dc commit 5f2c539

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

driver-core/src/main/com/mongodb/MongoTimeoutException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.annotations.Alpha;
2020
import com.mongodb.annotations.Reason;
21+
import com.mongodb.lang.Nullable;
2122

2223
/**
2324
* An exception indicating that the driver has timed out waiting for either a server or a connection to become available.
@@ -42,7 +43,7 @@ public MongoTimeoutException(final String message) {
4243
* @since 5.2
4344
*/
4445
@Alpha(Reason.CLIENT)
45-
public MongoTimeoutException(final String message, final Throwable cause) {
46+
public MongoTimeoutException(final String message, @Nullable final Throwable cause) {
4647
super(message, cause);
4748
}
4849
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private PooledConnection getPooledConnection(final Timeout waitQueueTimeout, fin
345345
}
346346
return new PooledConnection(internalConnection);
347347
} catch (MongoTimeoutException e) {
348-
throw createTimeoutException(startTime);
348+
throw createTimeoutException(startTime, e);
349349
}
350350
}
351351

@@ -359,13 +359,14 @@ private PooledConnection getPooledConnectionImmediate() {
359359
return internalConnection == null ? null : new PooledConnection(internalConnection);
360360
}
361361

362-
private MongoTimeoutException createTimeoutException(final StartTime startTime) {
362+
private MongoTimeoutException createTimeoutException(final StartTime startTime, @Nullable final MongoTimeoutException cause) {
363363
long elapsedMs = startTime.elapsed().toMillis();
364364
int numPinnedToCursor = pinnedStatsManager.getNumPinnedToCursor();
365365
int numPinnedToTransaction = pinnedStatsManager.getNumPinnedToTransaction();
366366
if (numPinnedToCursor == 0 && numPinnedToTransaction == 0) {
367367
return new MongoTimeoutException(format("Timed out after %d ms while waiting for a connection to server %s.",
368-
elapsedMs, serverId.getAddress()));
368+
elapsedMs, serverId.getAddress()),
369+
cause);
369370
} else {
370371
int maxSize = pool.getMaxSize();
371372
int numInUse = pool.getInUseCount();
@@ -375,7 +376,7 @@ private MongoTimeoutException createTimeoutException(final StartTime startTime)
375376
* - numInUse > 0
376377
* we consider at least one of `numPinnedToCursor`, `numPinnedToTransaction` to be positive,
377378
* so if we observe `numInUse` to be 0, we have to estimate it based on `numPinnedToCursor` and `numPinnedToTransaction`;
378-
* - numInUse < maxSize
379+
* - numInUse <= maxSize
379380
* `numInUse` must not exceed the limit in situations when we estimate `numInUse`;
380381
* - numPinnedToCursor + numPinnedToTransaction <= numInUse
381382
* otherwise the numbers do not make sense.
@@ -399,7 +400,8 @@ private MongoTimeoutException createTimeoutException(final StartTime startTime)
399400
+ "connections in use by other operations: %d",
400401
elapsedMs, serverId.getAddress(),
401402
sizeToString(maxSize), numPinnedToCursor, numPinnedToTransaction,
402-
numOtherInUse));
403+
numOtherInUse),
404+
cause);
403405
}
404406
}
405407

@@ -1067,7 +1069,7 @@ private PooledConnection acquirePermitOrGetAvailableOpenedConnection(final boole
10671069
& (availableConnection = tryGetAvailable ? tryGetAvailableConnection() : null) == null) {
10681070

10691071
Timeout.onExistsAndExpired(waitQueueTimeout, () -> {
1070-
throw createTimeoutException(startTime);
1072+
throw createTimeoutException(startTime, null);
10711073
});
10721074
waitQueueTimeout.awaitOn(permitAvailableOrHandedOverOrClosedOrPausedCondition,
10731075
() -> "acquiring permit or getting available opened connection");
@@ -1406,7 +1408,7 @@ void failAsClosed() {
14061408
}
14071409

14081410
void failAsTimedOut() {
1409-
doComplete(() -> createTimeoutException(startTime));
1411+
doComplete(() -> createTimeoutException(startTime, null));
14101412
}
14111413

14121414
private void doComplete(final Supplier<RuntimeException> failureSupplier) {

0 commit comments

Comments
 (0)