Skip to content

Commit

Permalink
[POOL-419] Fix for POOL-419. Before adding the returned object to the…
Browse files Browse the repository at this point in the history
… pool, we must check whether it is invalid.
  • Loading branch information
Raju Kumar Gupta committed Feb 3, 2025
1 parent 91a4d4d commit 6014819
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1091,18 +1091,17 @@ public void returnObject(final T obj) {
swallowException(e);
}
} else {

synchronized (p) {
if(p.getState() == PooledObjectState.INVALID) {
return;
}
if (getLifo()) {
idleObjects.addFirst(p);
} else {
idleObjects.addLast(p);
if(p.getState() != PooledObjectState.INVALID) {
if (getLifo()) {
idleObjects.addFirst(p);
} else {
idleObjects.addLast(p);
}
}
}


if (isClosed()) {
// Pool closed while object was being added to idle objects.
// Make sure the returned object is destroyed rather than left
Expand Down
30 changes: 19 additions & 11 deletions src/test/java/org/apache/commons/pool3/TestPoolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ void testPool419() throws Exception{

final GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();

config.setMaxTotal(10000);
config.setMaxIdle(10000);
config.setMinIdle(10000);
config.setMaxTotal(1000);
config.setMaxIdle(1000);
config.setMinIdle(1000);

@SuppressWarnings("unchecked")
final PooledObjectFactory<Object, RuntimeException> pof = createProxy(PooledObjectFactory.class, calledMethods);
Expand All @@ -716,24 +716,32 @@ void testPool419() throws Exception{

CountDownLatch startLatch = new CountDownLatch(1);

for (int i = 0; i < 10000; i++) {
Object poolObject = connectionPool.borrowObject();
List<Object> poolObjects = new ArrayList<>();

FutureTask<Boolean> invalidateObject = new FutureTask<>(() -> {
List<FutureTask<Boolean>> tasks = new ArrayList<>();

for (int i = 0; i < 1000; i++) {
poolObjects.add(connectionPool.borrowObject());
}

for(Object poolObject : poolObjects) {

tasks.add(new FutureTask<>(() -> {
startLatch.await();
connectionPool.invalidateObject(poolObject);
return true;
});
}));

FutureTask<Boolean> returnObject = new FutureTask<>(() -> {
tasks.add(new FutureTask<>(() -> {
startLatch.await();
connectionPool.returnObject(poolObject);
return true;
});
}));
}

executor.submit(returnObject);
executor.submit(invalidateObject);

for(FutureTask<Boolean> task : tasks) {
executor.submit(task);
}

startLatch.countDown(); // Start all tasks simultaneously
Expand Down

0 comments on commit 6014819

Please sign in to comment.