Skip to content

Commit b5411d3

Browse files
committed
- swallow factory exceptions in invalidate
- switch to addIdleObect and check takeWaiters - synchronization to guard concurrent invalidations on same instance
1 parent 5fbdf44 commit b5411d3

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,13 @@ public void invalidateObject(final T obj) throws Exception {
913913
* {@inheritDoc}
914914
* <p>
915915
* Activation of this method decrements the active count and attempts to destroy the instance, using the provided
916-
* {@link DestroyMode}. To ensure liveness of the pool, {@link #addObject()} is called to replace the invalidated
917-
* instance.
916+
* {@link DestroyMode}. To ensure liveness of the pool, when threads are waiting to borrow, a non-blocking
917+
* attempt is made to create a replacement idle instance via {@link #addIdleObject(PooledObject)} with
918+
* {@link Duration#ZERO}, directly bypassing the {@link #getMaxIdle()} gate that {@link #addObject()} enforces.
919+
* Replenishment is only attempted when the object is actually destroyed (i.e. not already in
920+
* {@link PooledObjectState#INVALID} state). Any exception thrown by the factory during replenishment
921+
* is swallowed via {@link #swallowException(Exception)} so that the invalidation itself is never aborted
922+
* by a factory failure.
918923
* </p>
919924
*
920925
* @throws Exception if an exception occurs destroying the object
@@ -933,11 +938,15 @@ public void invalidateObject(final T obj, final DestroyMode destroyMode) throws
933938
synchronized (p) {
934939
if (p.getState() != PooledObjectState.INVALID) {
935940
destroy(p, destroyMode);
941+
if (!isClosed() && idleObjects.hasTakeWaiters()) {
942+
try {
943+
addIdleObject(create(Duration.ZERO));
944+
} catch (final Exception e) {
945+
swallowException(e);
946+
}
947+
}
936948
}
937949
}
938-
if (!isClosed()) {
939-
addObject();
940-
}
941950
}
942951

943952
/**

0 commit comments

Comments
 (0)