Skip to content

Commit dea608e

Browse files
committed
fix issue #3524
1 parent 27d644b commit dea608e

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -389,11 +389,10 @@ private int assembleConnectionTypeCode(String url, String username, String passw
389389
}
390390

391391
protected void pushConnection(PooledConnection conn) throws SQLException {
392-
393392
lock.lock();
394393
try {
395-
state.activeConnections.remove(conn);
396394
if (conn.isValid()) {
395+
state.activeConnections.remove(conn);
397396
if (state.idleConnections.size() < poolMaximumIdleConnections
398397
&& conn.getConnectionTypeCode() == expectedConnectionTypeCode) {
399398
state.accumulatedCheckoutTime += conn.getCheckoutTime();
@@ -441,17 +440,19 @@ private PooledConnection popConnection(String username, String password) throws
441440
while (conn == null) {
442441
lock.lock();
443442
try {
444-
if (!state.idleConnections.isEmpty()) {
445-
// Pool has available connection
446-
conn = state.idleConnections.remove(0);
447-
if (log.isDebugEnabled()) {
448-
log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
449-
}
450-
} else if (state.activeConnections.size() < poolMaximumActiveConnections) {
451-
// Pool does not have available connection and can create a new connection
452-
conn = new PooledConnection(dataSource.getConnection(), this);
453-
if (log.isDebugEnabled()) {
454-
log.debug("Created connection " + conn.getRealHashCode() + ".");
443+
if (state.activeConnections.size() < poolMaximumActiveConnections) {
444+
if (!state.idleConnections.isEmpty()) {
445+
// Pool has available connection
446+
conn = state.idleConnections.remove(0);
447+
if (log.isDebugEnabled()) {
448+
log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
449+
}
450+
} else {
451+
// Pool does not have available connection and can create a new connection
452+
conn = new PooledConnection(dataSource.getConnection(), this);
453+
if (log.isDebugEnabled()) {
454+
log.debug("Created connection " + conn.getRealHashCode() + ".");
455+
}
455456
}
456457
} else {
457458
// Cannot create new connection

src/test/java/org/apache/ibatis/datasource/pooled/PooledDataSourceTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,4 +158,27 @@ void forceCloseAllShouldRemoveAllActiveAndIdleConnection() throws SQLException {
158158
assertEquals(0, poolState.getActiveConnectionCount());
159159
assertEquals(0, poolState.getIdleConnectionCount());
160160
}
161+
162+
@Test
163+
void shouldActiveConnectionLessThanOrEqualPoolMaximumActiveConnections() throws Exception {
164+
dataSource.setPoolMaximumCheckoutTime(1000);
165+
dataSource.setPoolTimeToWait(500);
166+
dataSource.setPoolMaximumActiveConnections(1);
167+
dataSource.setPoolMaximumIdleConnections(1);
168+
169+
Connection conn1 = dataSource.getConnection();
170+
conn1.setAutoCommit(false);
171+
Connection conn2 = dataSource.getConnection();
172+
conn2.setAutoCommit(false);
173+
conn1.close();
174+
Connection conn3 = dataSource.getConnection();
175+
conn3.setAutoCommit(false);
176+
conn2.close();
177+
Connection conn4 = dataSource.getConnection();
178+
conn4.setAutoCommit(false);
179+
PoolState poolState = dataSource.getPoolState();
180+
assertTrue(poolState.activeConnections.size() <= poolState.dataSource.poolMaximumActiveConnections);
181+
conn3.close();
182+
conn4.close();
183+
}
161184
}

0 commit comments

Comments
 (0)