2626 */
2727final class ConnectionPool implements DataSourcePool {
2828
29+ @ FunctionalInterface
30+ interface Heartbeat {
31+
32+ void stop ();
33+ }
34+
2935 private static final String APPLICATION_NAME = "ApplicationName" ;
3036 private final ReentrantLock heartbeatLock = new ReentrantLock (false );
3137 private final ReentrantLock notifyLock = new ReentrantLock (false );
@@ -80,7 +86,7 @@ final class ConnectionPool implements DataSourcePool {
8086 private final int waitTimeoutMillis ;
8187 private final int pstmtCacheSize ;
8288 private final PooledConnectionQueue queue ;
83- private Timer heartBeatTimer ;
89+ private Heartbeat heartbeat ;
8490 private int heartbeatPoolExhaustedCount ;
8591 private final ExecutorService executor ;
8692
@@ -161,13 +167,6 @@ void pstmtCacheMetrics(PstmtCache pstmtCache) {
161167 pscRem .add (pstmtCache .removeCount ());
162168 }
163169
164- final class HeartBeatRunnable extends TimerTask {
165- @ Override
166- public void run () {
167- heartBeat ();
168- }
169- }
170-
171170 @ Override
172171 public java .util .logging .Logger getParentLogger () throws SQLFeatureNotSupportedException {
173172 throw new SQLFeatureNotSupportedException ("We do not support java.util.logging" );
@@ -387,7 +386,7 @@ private void trimIdleConnections() {
387386 * This is called by the HeartbeatRunnable which should be scheduled to
388387 * run periodically (every heartbeatFreqSecs seconds).
389388 */
390- private void heartBeat () {
389+ void heartbeat () {
391390 trimIdleConnections ();
392391 if (validateOnHeartbeat ) {
393392 testConnection ();
@@ -727,11 +726,10 @@ private void startHeartBeatIfStopped() {
727726 heartbeatLock .lock ();
728727 try {
729728 // only start if it is not already running
730- if (heartBeatTimer == null ) {
729+ if (heartbeat == null ) {
731730 int freqMillis = heartbeatFreqSecs * 1000 ;
732731 if (freqMillis > 0 ) {
733- heartBeatTimer = new Timer (name + ".heartBeat" , true );
734- heartBeatTimer .scheduleAtFixedRate (new HeartBeatRunnable (), freqMillis , freqMillis );
732+ heartbeat = ExecutorFactory .newHeartBeat (this , freqMillis );
735733 }
736734 }
737735 } finally {
@@ -743,9 +741,9 @@ private void stopHeartBeatIfRunning() {
743741 heartbeatLock .lock ();
744742 try {
745743 // only stop if it was running
746- if (heartBeatTimer != null ) {
747- heartBeatTimer . cancel ();
748- heartBeatTimer = null ;
744+ if (heartbeat != null ) {
745+ heartbeat . stop ();
746+ heartbeat = null ;
749747 }
750748 } finally {
751749 heartbeatLock .unlock ();
0 commit comments