Skip to content

Commit 0c8597d

Browse files
Adjust tests and CPDSConnectionFactory#invalidate to accomodate behav… (#539)
* Adjust tests and CPDSConnectionFactory#invalidate to accomodate behavior change in the fix for POOL-424. * Remove `ex.printStackTrace();` --------- Co-authored-by: Gary Gregory <[email protected]>
1 parent d86c1ae commit 0c8597d

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ The <action> type attribute can be add,update,fix,remove.
121121
<!-- ADD -->
122122
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.dbcp2.datasources.PooledConnectionManager.setPassword(char[]).</action>
123123
<!-- UPDATE -->
124+
<action type="update" dev="psteitz">Update tests and CPDSConnectionFactory#invalidate to accomodate changed behavior in the fix for POOL-424.</action>
124125
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 78 to 93 #521, #537, #538.</action>
125126
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-pool2 from 2.12.0 to 2.12.1 #474.</action>
126127
<action type="update" dev="ggregory" due-to="Gary Gregory">Port site from Doxia 1 to 2.</action>

src/main/java/org/apache/commons/dbcp2/datasources/CPDSConnectionFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ public void invalidate(final PooledConnection pc) throws SQLException {
192192
throw new IllegalStateException(NO_KEY_MESSAGE);
193193
}
194194
try {
195-
pool.invalidateObject(pci); // Destroy instance and update pool counters
196195
pool.close(); // Clear any other instances in this pool and kill others as they come back
196+
// Calling close before invalidate ensures that invalidate will not trigger a create attempt
197+
pool.invalidateObject(pci); // Destroy instance and update pool counters
197198
} catch (final Exception ex) {
198199
throw new SQLException("Error invalidating connection", ex);
199200
}

src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,37 @@ void testInstanceNotFoundExceptionLogSuppressed() throws Exception {
568568
assertNull(ds.getRegisteredJmxName());
569569
}
570570

571+
/**
572+
* Cycle through idle connections and verify that they are all valid.
573+
* Assumes we are the only client of the pool.
574+
*
575+
* @throws Exception if an error occurs
576+
*/
577+
private void checkIdleValid() throws Exception {
578+
final Set<Connection> idleConnections = new HashSet<>(); // idle connections
579+
// Get idle connections by repeatedly making connection requests up to NumIdle
580+
for (int i = 0; i < ds.getNumIdle(); i++) {
581+
final Connection conn = ds.getConnection();
582+
idleConnections.add(conn);
583+
}
584+
// Cycle through idle connections and verify that they are valid
585+
for (final Connection conn : idleConnections) {
586+
assertTrue(conn.isValid(2), "Connection should be valid");
587+
conn.close();
588+
}
589+
}
590+
591+
/**
592+
* Check that maxTotal and maxIdle are not exceeded
593+
*
594+
* @throws Exception
595+
*/
596+
private void checkLimits() throws Exception {
597+
assertTrue(ds.getNumActive() + ds.getNumIdle() <= getMaxTotal(), "Total connections exceed maxTotal");
598+
assertTrue(ds.getNumIdle() <= ds.getMaxIdle(), "Idle connections exceed maxIdle");
599+
}
600+
601+
571602
@Test
572603
void testInvalidateConnection() throws Exception {
573604
ds.setMaxTotal(2);
@@ -576,9 +607,11 @@ void testInvalidateConnection() throws Exception {
576607
ds.invalidateConnection(conn1);
577608
assertTrue(conn1.isClosed());
578609
assertEquals(1, ds.getNumActive());
579-
assertEquals(0, ds.getNumIdle());
610+
checkIdleValid();
611+
checkLimits();
580612
try (final Connection conn3 = ds.getConnection()) {
581613
conn2.close();
614+
conn3.close();
582615
}
583616
}
584617
}

src/test/java/org/apache/commons/dbcp2/TestPoolingDriver.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ void testInvalidateConnection() throws Exception {
142142
driver2.invalidateConnection(conn);
143143

144144
assertEquals(0, pool.getNumActive());
145-
assertEquals(0, pool.getNumIdle());
146145
assertTrue(conn.isClosed());
147146
}
148147

src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public void setUp() throws Exception {
5050
delegate.setPassword("password");
5151
}
5252

53+
private void checkPoolLimits(final GenericObjectPool<PooledConnectionAndInfo> pool) {
54+
assertTrue(pool.getNumActive() + pool.getNumIdle() <= pool.getMaxTotal(),
55+
"Active + Idle should be <= MaxTotal");
56+
assertTrue(pool.getNumIdle() <= pool.getMaxIdle(), "Idle should be <= MaxIdle");
57+
}
58+
5359
/**
5460
* JIRA DBCP-216
5561
*
@@ -77,14 +83,14 @@ void testConnectionErrorCleanup() throws Exception {
7783
// Throw connectionError event
7884
pc.throwConnectionError();
7985

80-
// Active count should be reduced by 1 and no idle increase
86+
// Active count should be reduced by 1
8187
assertEquals(1, pool.getNumActive());
82-
assertEquals(0, pool.getNumIdle());
88+
checkPoolLimits(pool);
8389

8490
// Throw another one - should be ignored
8591
pc.throwConnectionError();
8692
assertEquals(1, pool.getNumActive());
87-
assertEquals(0, pool.getNumIdle());
93+
checkPoolLimits(pool);
8894

8995
// Ask for another connection
9096
final PooledConnection pcon3 = pool.borrowObject().getPooledConnection();

0 commit comments

Comments
 (0)