Skip to content

Commit 1e77bc9

Browse files
committed
#31 - datasource.offline() closes busy connections ... should let them close on return
1 parent d9a1639 commit 1e77bc9

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,16 +788,20 @@ public void shutdown() {
788788
*/
789789
@Override
790790
public void shutdown(boolean deregisterDriver) {
791-
offline();
791+
shutdownPool(true);
792792
if (deregisterDriver) {
793793
deregisterDriver();
794794
}
795795
}
796796

797797
@Override
798798
public void offline() {
799+
shutdownPool(false);
800+
}
801+
802+
private void shutdownPool(boolean closeBusyConnections) {
799803
stopHeartBeatIfRunning();
800-
queue.shutdown();
804+
queue.shutdown(closeBusyConnections);
801805
dataSourceUp.set(false);
802806
}
803807

src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private PooledConnection _getPooledConnectionWaitLoop() throws SQLException, Int
311311
}
312312
}
313313

314-
public void shutdown() {
314+
void shutdown(boolean closeBusyConnections) {
315315
lock.lock();
316316
try {
317317
doingShutdown = true;
@@ -320,10 +320,15 @@ public void shutdown() {
320320

321321
closeFreeConnections(true);
322322

323-
if (!busyList.isEmpty()) {
324-
logger.warn("Closing busy connections on shutdown size: " + busyList.size());
325-
dumpBusyConnectionInformation();
326-
closeBusyConnections(0);
323+
if (!closeBusyConnections) {
324+
// connections close on return to pool
325+
lastResetTime = System.currentTimeMillis() - 100;
326+
} else {
327+
if (!busyList.isEmpty()) {
328+
logger.warn("Closing busy connections on shutdown size: " + busyList.size());
329+
dumpBusyConnectionInformation();
330+
closeBusyConnections(0);
331+
}
327332
}
328333
} finally {
329334
lock.unlock();

src/test/java/io/ebean/datasource/pool/ConnectionPoolOfflineTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.testng.annotations.Test;
77

88
import java.sql.Connection;
9+
import java.sql.PreparedStatement;
910
import java.sql.SQLException;
1011

1112
import static org.assertj.core.api.Assertions.assertThat;
@@ -89,6 +90,48 @@ public void offlineOffline() {
8990
assertThat(pool.isOnline()).isFalse();
9091
}
9192

93+
@Test
94+
public void offline_whenBusy_allowed() throws SQLException, InterruptedException {
95+
96+
DataSourceConfig config = config().setUrl("jdbc:h2:mem:offlineWhenBusy");
97+
98+
ConnectionPool pool = new ConnectionPool("offlineWhenBusy", config);
99+
pool.online();
100+
101+
final Connection busy = pool.getConnection();
102+
Thread thread = new Thread(() -> {
103+
try {
104+
System.out.println("busy connection being used");
105+
try (PreparedStatement statement = busy.prepareStatement("select 'hello' from dual")) {
106+
statement.execute();
107+
}
108+
Thread.sleep(3000);
109+
System.out.println("busy connection closing now");
110+
busy.close();
111+
} catch (SQLException | InterruptedException e) {
112+
e.printStackTrace();
113+
throw new RuntimeException("Should not fail!!");
114+
}
115+
});
116+
117+
thread.start();
118+
119+
Thread.sleep(200);
120+
System.out.println("-- taking pool offline (with a busy connection)");
121+
assertEquals(1, pool.getStatus(false).getBusy());
122+
123+
pool.offline();
124+
assertEquals(0, pool.getStatus(false).getFree());
125+
assertEquals(1, pool.getStatus(false).getBusy()); // still 1 busy connection
126+
127+
// a bit of time to let busy connection finish and close
128+
Thread.sleep(4000);
129+
130+
// all done now
131+
assertEquals(0, pool.getStatus(false).getFree());
132+
assertEquals(0, pool.getStatus(false).getBusy());
133+
}
134+
92135
@Test
93136
public void offlineGetConnection_expect_goesOnline() throws SQLException {
94137

0 commit comments

Comments
 (0)