Skip to content

Commit c5d63ad

Browse files
committed
Fix for JIRA: POOL-425. Make addObject no-op when maxIdle is attained.
1 parent acc2b95 commit c5d63ad

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<release version="2.13.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4949
<!-- FIX -->
50-
<action type="fix" issue="POOL-350" dev="psteitz" due-to="Phil Steitz">Make placement of calls to GKOP reuseCapacity configurable.</action>
50+
<action type="fix" issue="POOL-425" dev="psteitz">GenericObjectPool addObject does not respect maxIdle.</action>
51+
<action type="fix" issue="POOL-350" dev="psteitz">Make placement of calls to GKOP reuseCapacity configurable.</action>
5152
<action type="fix" issue="POOL-290" dev="psteitz" due-to="Serge Angelov">TestSoftRefOutOfMemory (unit test) can loop infinitely on failure.</action>
5253
<action type="fix" issue="POOL-419" dev="psteitz" due-to="Raju Gupta, Phil Steitz">GenericObjectPool counters and object collections can be corrupted when returnObject and invalidate are invoked concurrently by client threads on the same pooled object.</action>
5354
<action type="fix" issue="POOL-421" dev="psteitz" due-to="Phil Steitz">GenericObjectPool addObject should return immediately when there is no capacity to add.</action>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ private void addIdleObject(final PooledObject<T> p) throws Exception {
193193
}
194194

195195
/**
196-
* Creates an object, and place it into the pool. addObject() is useful for
196+
* Creates an object, and places it into the pool. addObject() is useful for
197197
* "pre-loading" a pool with idle objects.
198198
* <p>
199-
* If there is no capacity available to add to the pool, this is a no-op
199+
* If there is no capacity available to add to the pool, or there are already
200+
* {@link #getMaxIdle()} idle instances in the pool, this is a no-op
200201
* (no exception, no impact to the pool).
201202
* </p>
202203
* <p>
@@ -213,7 +214,8 @@ public void addObject() throws Exception {
213214
}
214215

215216
final int localMaxTotal = getMaxTotal();
216-
if (localMaxTotal < 0 || createCount.get() < localMaxTotal) {
217+
final int localMaxIdle = getMaxIdle();
218+
if (getNumIdle() < localMaxIdle && (localMaxTotal < 0 || createCount.get() < localMaxTotal)) {
217219
addIdleObject(create(getMaxWaitDuration()));
218220
}
219221
}

src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ private void checkEvictorVisiting(final boolean lifo) throws Exception {
750750
trackerPool.setMaxIdle(-1);
751751
final int instanceCount = 10 + random.nextInt(20);
752752
trackerPool.setMaxTotal(instanceCount);
753+
trackerPool.setMaxIdle(instanceCount);
753754
for (int k = 0; k < instanceCount; k++) {
754755
trackerPool.addObject();
755756
}
@@ -2980,4 +2981,32 @@ void testWhenExhaustedFail() throws Exception {
29802981
assertEquals(1, genericObjectPool.getNumIdle());
29812982
genericObjectPool.close();
29822983
}
2984+
2985+
@Test
2986+
void testAddObjectRespectsMaxIdle() throws Exception {
2987+
genericObjectPool.setMaxIdle(1);
2988+
genericObjectPool.addObject();
2989+
genericObjectPool.addObject(); // should be no-op
2990+
assertEquals(1, genericObjectPool.getNumIdle());
2991+
}
2992+
2993+
@Test
2994+
void testAddObjectRespectsMaxTotal() throws Exception {
2995+
genericObjectPool.setMaxTotal(1);
2996+
genericObjectPool.addObject();
2997+
genericObjectPool.addObject(); // should be no-op
2998+
assertEquals(1, genericObjectPool.getNumIdle());
2999+
}
3000+
3001+
@Test
3002+
void testAddObjectCanAddToMaxIdle() throws Exception {
3003+
genericObjectPool.setMaxTotal(5);
3004+
genericObjectPool.borrowObject();
3005+
genericObjectPool.borrowObject();
3006+
genericObjectPool.setMaxIdle(3);
3007+
for (int i = 0; i < 3; i++) {
3008+
genericObjectPool.addObject();
3009+
}
3010+
assertEquals(3, genericObjectPool.getNumIdle());
3011+
}
29833012
}

0 commit comments

Comments
 (0)