Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
final class ConnectionPool implements DataSourcePool {

@FunctionalInterface
interface Heartbeat {

void stop();
}

private static final String APPLICATION_NAME = "ApplicationName";
private final ReentrantLock heartbeatLock = new ReentrantLock(false);
private final ReentrantLock notifyLock = new ReentrantLock(false);
Expand Down Expand Up @@ -80,7 +86,7 @@ final class ConnectionPool implements DataSourcePool {
private final int waitTimeoutMillis;
private final int pstmtCacheSize;
private final PooledConnectionQueue queue;
private Timer heartBeatTimer;
private Heartbeat heartbeat;
private int heartbeatPoolExhaustedCount;
private final ExecutorService executor;

Expand Down Expand Up @@ -161,13 +167,6 @@ void pstmtCacheMetrics(PstmtCache pstmtCache) {
pscRem.add(pstmtCache.removeCount());
}

final class HeartBeatRunnable extends TimerTask {
@Override
public void run() {
heartBeat();
}
}

@Override
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("We do not support java.util.logging");
Expand Down Expand Up @@ -387,7 +386,7 @@ private void trimIdleConnections() {
* This is called by the HeartbeatRunnable which should be scheduled to
* run periodically (every heartbeatFreqSecs seconds).
*/
private void heartBeat() {
void heartbeat() {
trimIdleConnections();
if (validateOnHeartbeat) {
testConnection();
Expand Down Expand Up @@ -727,11 +726,10 @@ private void startHeartBeatIfStopped() {
heartbeatLock.lock();
try {
// only start if it is not already running
if (heartBeatTimer == null) {
if (heartbeat == null) {
int freqMillis = heartbeatFreqSecs * 1000;
if (freqMillis > 0) {
heartBeatTimer = new Timer(name + ".heartBeat", true);
heartBeatTimer.scheduleAtFixedRate(new HeartBeatRunnable(), freqMillis, freqMillis);
heartbeat = ExecutorFactory.newHeartBeat(this, freqMillis);
}
}
} finally {
Expand All @@ -743,9 +741,9 @@ private void stopHeartBeatIfRunning() {
heartbeatLock.lock();
try {
// only stop if it was running
if (heartBeatTimer != null) {
heartBeatTimer.cancel();
heartBeatTimer = null;
if (heartbeat != null) {
heartbeat.stop();
heartbeat = null;
}
} finally {
heartbeatLock.unlock();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.ebean.datasource.pool;

import io.ebean.datasource.pool.ConnectionPool.Heartbeat;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -8,4 +12,41 @@ final class ExecutorFactory {
static ExecutorService newExecutor() {
return Executors.newSingleThreadExecutor();
}

/**
* Return a new Heartbeat for the pool.
*/
static Heartbeat newHeartBeat(ConnectionPool pool, int freqMillis) {
final Timer timer = new Timer(pool.name() + ".heartbeat", true);
timer.scheduleAtFixedRate(new HeartbeatTask(pool), freqMillis, freqMillis);
return new TimerHeartbeat(timer);
}

private static final class TimerHeartbeat implements Heartbeat {

private final Timer timer;

private TimerHeartbeat(Timer timer) {
this.timer = timer;
}

@Override
public void stop() {
timer.cancel();
}
}

private static final class HeartbeatTask extends TimerTask {

private final ConnectionPool pool;

private HeartbeatTask(ConnectionPool pool) {
this.pool = pool;
}

@Override
public void run() {
pool.heartbeat();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
package io.ebean.datasource.pool;

import io.ebean.datasource.pool.ConnectionPool.Heartbeat;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool: I didn't know this method until now:
Rolands note: Classes will be located in META-INF/versions/21
https://docs.oracle.com/javase/10/docs/specs/jar/jar.html#multi-release-jar-files


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

final class ExecutorFactory {

static ExecutorService newExecutor() {
return Executors.newVirtualThreadPerTaskExecutor();
}

static Heartbeat newHeartBeat(ConnectionPool pool, int freqMillis) {
return new VTHeartbeat(pool, freqMillis).start();
}

private static final class VTHeartbeat implements Heartbeat {

private final AtomicBoolean running = new AtomicBoolean(false);
private final ConnectionPool pool;
private final int freqMillis;
private final Thread thread;

private VTHeartbeat(ConnectionPool pool, int freqMillis) {
this.pool = pool;
this.freqMillis = freqMillis;
this.thread = Thread.ofVirtual()
.name(pool.name() + ".heartbeat")
.unstarted(this::run);
}

private void run() {
while (running.get()) {
try {
Thread.sleep(freqMillis);
pool.heartbeat();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (Exception e) {
// continue heartbeat
Log.warn("Error during heartbeat", e);
}
}
}

private Heartbeat start() {
running.set(true);
thread.start();
return this;
}

@Override
public void stop() {
running.set(false);
thread.interrupt();
}
}
}
Loading