Skip to content

Commit 665bc95

Browse files
committed
#23 - Tidy up online() online() and offline() offline() behaviour + API 4.5 improvements
1 parent b19b7f7 commit 665bc95

File tree

3 files changed

+103
-24
lines changed

3 files changed

+103
-24
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>io.ebean</groupId>
1212
<artifactId>ebean-datasource</artifactId>
13-
<version>4.4.2-SNAPSHOT</version>
13+
<version>4.5.1-SNAPSHOT</version>
1414

1515
<scm>
1616
<developerConnection>scm:git:[email protected]:ebean-orm/ebean-datasource.git</developerConnection>
@@ -22,7 +22,7 @@
2222
<dependency>
2323
<groupId>io.ebean</groupId>
2424
<artifactId>ebean-datasource-api</artifactId>
25-
<version>4.4</version>
25+
<version>4.5</version>
2626
</dependency>
2727

2828
<dependency>

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

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,6 @@ private void checkDriver() {
280280
}
281281
}
282282

283-
@Override
284-
public void online() throws SQLException {
285-
initialise();
286-
}
287-
288283
private void initialise() throws SQLException {
289284

290285
//noinspection StringBufferReplaceableByString
@@ -300,7 +295,7 @@ private void initialise() throws SQLException {
300295
try {
301296
dataSourceUp = true;
302297
queue.ensureMinimumConnections();
303-
startHeartBeat();
298+
startHeartBeatIfStopped();
304299
} catch (SQLException e) {
305300
if (failOnStart) {
306301
throw e;
@@ -309,14 +304,6 @@ private void initialise() throws SQLException {
309304
}
310305
}
311306

312-
private void startHeartBeat() {
313-
int freqMillis = heartbeatFreqSecs * 1000;
314-
if (freqMillis > 0) {
315-
heartBeatTimer = new Timer(name + ".heartBeat", true);
316-
heartBeatTimer.scheduleAtFixedRate(new HeartBeatRunnable(), freqMillis, freqMillis);
317-
}
318-
}
319-
320307
/**
321308
* Initialise the database using the owner credentials if we can't connect using the normal credentials.
322309
* <p>
@@ -438,6 +425,7 @@ private synchronized void notifyDataSourceIsUp() {
438425
dataSourceUp = true;
439426
dataSourceDownReason = null;
440427
reset();
428+
startHeartBeatIfStopped();
441429
}
442430
}
443431

@@ -845,6 +833,11 @@ public void testAlert() {
845833
}
846834
}
847835

836+
@Override
837+
public void shutdown() {
838+
shutdown(false);
839+
}
840+
848841
/**
849842
* This will close all the free connections, and then go into a wait loop,
850843
* waiting for the busy connections to be freed.
@@ -855,21 +848,51 @@ public void testAlert() {
855848
* </p>
856849
*/
857850
@Override
858-
public void shutdown(boolean deregisterDriver) {
859-
heartBeatTimer.cancel();
860-
queue.shutdown();
851+
public synchronized void shutdown(boolean deregisterDriver) {
852+
offline();
861853
if (deregisterDriver) {
862854
deregisterDriver();
863855
}
864856
}
865857

866858
@Override
867-
public void offline() {
868-
heartBeatTimer.cancel();
859+
public synchronized void offline() {
860+
stopHeartBeatIfRunning();
869861
queue.shutdown();
870862
dataSourceUp = false;
871863
}
872864

865+
@Override
866+
public synchronized boolean isOnline() {
867+
return dataSourceUp;
868+
}
869+
870+
@Override
871+
public synchronized void online() throws SQLException {
872+
if (!dataSourceUp) {
873+
initialise();
874+
}
875+
}
876+
877+
private void startHeartBeatIfStopped() {
878+
// only start if it is not already running
879+
if (heartBeatTimer == null) {
880+
int freqMillis = heartbeatFreqSecs * 1000;
881+
if (freqMillis > 0) {
882+
heartBeatTimer = new Timer(name + ".heartBeat", true);
883+
heartBeatTimer.scheduleAtFixedRate(new HeartBeatRunnable(), freqMillis, freqMillis);
884+
}
885+
}
886+
}
887+
888+
private void stopHeartBeatIfRunning() {
889+
// only stop if it was running
890+
if (heartBeatTimer != null) {
891+
heartBeatTimer.cancel();
892+
heartBeatTimer = null;
893+
}
894+
}
895+
873896
/**
874897
* Return the default autoCommit setting Connections in this pool will use.
875898
*

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import org.slf4j.LoggerFactory;
66
import org.testng.annotations.Test;
77

8+
import java.sql.Connection;
89
import java.sql.SQLException;
910

11+
import static org.assertj.core.api.Assertions.assertThat;
1012
import static org.testng.Assert.assertEquals;
1113

1214
public class ConnectionPoolOfflineTest {
@@ -17,7 +19,7 @@ private DataSourceConfig config() {
1719

1820
DataSourceConfig config = new DataSourceConfig();
1921
config.setDriver("org.h2.Driver");
20-
config.setUrl("jdbc:h2:mem:tests");
22+
config.setUrl("jdbc:h2:mem:testOffline");
2123
config.setUsername("sa");
2224
config.setPassword("");
2325
config.setMinConnections(2);
@@ -33,7 +35,8 @@ public void testOffline() throws InterruptedException, SQLException {
3335

3436
DataSourceConfig config = config();
3537

36-
ConnectionPool pool = new ConnectionPool("test", config);
38+
ConnectionPool pool = new ConnectionPool("testOffline", config);
39+
assertThat(pool.isOnline()).isFalse();
3740
log.info("pool created ");
3841
Thread.sleep(3000);
3942

@@ -42,28 +45,81 @@ public void testOffline() throws InterruptedException, SQLException {
4245

4346
pool.online();
4447
log.info("pool online");
48+
assertThat(pool.isOnline()).isTrue();
4549
assertEquals(2, pool.getStatus(false).getFree());
4650
assertEquals(0, pool.getStatus(false).getBusy());
4751

4852
Thread.sleep(3000);
4953

5054
pool.offline();
5155
log.info("pool offline");
56+
assertThat(pool.isOnline()).isFalse();
5257
assertEquals(0, pool.getStatus(false).getFree());
5358
assertEquals(0, pool.getStatus(false).getBusy());
5459

5560
Thread.sleep(3000);
5661

5762
pool.online();
5863
log.info("pool online");
64+
assertThat(pool.isOnline()).isTrue();
5965
assertEquals(2, pool.getStatus(false).getFree());
6066
assertEquals(0, pool.getStatus(false).getBusy());
6167
Thread.sleep(3000);
6268

63-
pool.shutdown(false);
69+
pool.shutdown();
6470

71+
assertThat(pool.isOnline()).isFalse();
6572
assertEquals(0, pool.getStatus(false).getFree());
6673
assertEquals(0, pool.getStatus(false).getBusy());
6774
}
6875

76+
@Test
77+
public void offlineOffline() {
78+
79+
DataSourceConfig config = config().setUrl("jdbc:h2:mem:offlineOffline");
80+
81+
ConnectionPool pool = new ConnectionPool("offlineOffline", config);
82+
assertThat(pool.isOnline()).isFalse();
83+
84+
pool.offline();
85+
assertThat(pool.isOnline()).isFalse();
86+
87+
pool.offline();
88+
pool.offline();
89+
assertThat(pool.isOnline()).isFalse();
90+
}
91+
92+
@Test
93+
public void offlineGetConnection_expect_goesOnline() throws SQLException {
94+
95+
DataSourceConfig config = config().setUrl("jdbc:h2:mem:offlineOffline");
96+
97+
ConnectionPool pool = new ConnectionPool("offlineOffline", config);
98+
pool.offline();
99+
assertThat(pool.isOnline()).isFalse();
100+
101+
try (Connection connection = pool.getConnection()) {
102+
assertThat(connection).isNotNull();
103+
assertThat(pool.isOnline()).isTrue();
104+
}
105+
106+
pool.shutdown();
107+
assertThat(pool.isOnline()).isFalse();
108+
}
109+
110+
@Test
111+
public void onlineOnline() throws SQLException {
112+
113+
DataSourceConfig config = config().setUrl("jdbc:h2:mem:onlineOnline");
114+
115+
ConnectionPool pool = new ConnectionPool("onlineOnline", config);
116+
assertThat(pool.isOnline()).isFalse();
117+
118+
pool.online();
119+
assertThat(pool.isOnline()).isTrue();
120+
121+
pool.online();
122+
pool.online();
123+
assertThat(pool.isOnline()).isTrue();
124+
}
69125
}

0 commit comments

Comments
 (0)