Skip to content

Commit e540e8c

Browse files
committed
#38 - Refactor to use ReentrantLock for hearbeat start/stop and notify up/down
1 parent a44b684 commit e540e8c

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.TimerTask;
2828
import java.util.concurrent.atomic.AtomicBoolean;
2929
import java.util.concurrent.atomic.AtomicInteger;
30+
import java.util.concurrent.locks.ReentrantLock;
3031

3132
/**
3233
* A robust DataSource implementation.
@@ -42,6 +43,8 @@ public class ConnectionPool implements DataSourcePool {
4243

4344
private static final Logger logger = LoggerFactory.getLogger(ConnectionPool.class);
4445

46+
private final ReentrantLock heartbeatLock = new ReentrantLock(false);
47+
private final ReentrantLock notifyLock = new ReentrantLock(false);
4548
/**
4649
* The name given to this dataSource.
4750
*/
@@ -134,11 +137,6 @@ public class ConnectionPool implements DataSourcePool {
134137
*/
135138
private long lastTrimTime;
136139

137-
/**
138-
* Synchronization monitor for the heartBeat timer.
139-
*/
140-
private final Object heartBeatMonitor = new Object();
141-
142140
/**
143141
* HeartBeat checking will discover when it goes down, and comes back up again.
144142
*/
@@ -417,7 +415,8 @@ private void notifyDataSourceIsDown(SQLException reason) {
417415
}
418416

419417
private void notifyDown(SQLException reason) {
420-
synchronized (dataSourceUp) {
418+
notifyLock.lock();
419+
try {
421420
if (dataSourceUp.get()) {
422421
// check and set false immediately so that we only alert once
423422
dataSourceUp.set(false);
@@ -427,6 +426,8 @@ private void notifyDown(SQLException reason) {
427426
notify.dataSourceDown(this, reason);
428427
}
429428
}
429+
} finally {
430+
notifyLock.unlock();
430431
}
431432
}
432433

@@ -438,7 +439,8 @@ private void notifyDataSourceIsUp() {
438439
}
439440

440441
private void notifyUp() {
441-
synchronized (dataSourceUp) {
442+
notifyLock.lock();
443+
try {
442444
// check such that we only notify once
443445
if (!dataSourceUp.get()) {
444446
dataSourceUp.set(true);
@@ -451,6 +453,8 @@ private void notifyUp() {
451453
} else {
452454
logger.info("DataSourcePool [" + name + "] is back up!");
453455
}
456+
} finally {
457+
notifyLock.unlock();
454458
}
455459
}
456460

@@ -847,7 +851,8 @@ public boolean isOnline() {
847851
}
848852

849853
private void startHeartBeatIfStopped() {
850-
synchronized (heartBeatMonitor) {
854+
heartbeatLock.lock();
855+
try {
851856
// only start if it is not already running
852857
if (heartBeatTimer == null) {
853858
int freqMillis = heartbeatFreqSecs * 1000;
@@ -856,16 +861,21 @@ private void startHeartBeatIfStopped() {
856861
heartBeatTimer.scheduleAtFixedRate(new HeartBeatRunnable(), freqMillis, freqMillis);
857862
}
858863
}
864+
} finally {
865+
heartbeatLock.unlock();
859866
}
860867
}
861868

862869
private void stopHeartBeatIfRunning() {
863-
synchronized (heartBeatMonitor) {
870+
heartbeatLock.lock();
871+
try {
864872
// only stop if it was running
865873
if (heartBeatTimer != null) {
866874
heartBeatTimer.cancel();
867875
heartBeatTimer = null;
868876
}
877+
} finally {
878+
heartbeatLock.unlock();
869879
}
870880
}
871881

0 commit comments

Comments
 (0)