set default timeout to 15 seconds

This commit is contained in:
Jörg Prante 2023-08-31 10:07:39 +02:00
parent 52097a4468
commit a7ac905757
8 changed files with 20 additions and 24 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = database name = database
version = 1.4.3 version = 1.4.4
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -176,7 +176,7 @@ public class Pool implements BagStateListener {
public void quietlyCloseConnection(Connection connection, String closureReason) { public void quietlyCloseConnection(Connection connection, String closureReason) {
if (connection != null) { if (connection != null) {
try { try {
logger.log(Level.FINE, () -> MessageFormat.format("{0} closing connection {1} {2}", poolName, connection, closureReason)); logger.log(Level.FINE, () -> MessageFormat.format("{0} closing connection {1} {2} with a timeout of 15 seconds", poolName, connection, closureReason));
try (connection) { try (connection) {
setNetworkTimeout(connection, TimeUnit.SECONDS.toMillis(15)); setNetworkTimeout(connection, TimeUnit.SECONDS.toMillis(15));
} catch (SQLException e) { } catch (SQLException e) {

View file

@ -92,14 +92,14 @@ public class PoolConfig {
this.properties = properties; this.properties = properties;
this.minIdle = -1; this.minIdle = -1;
this.maxPoolSize = -1; this.maxPoolSize = -1;
this.maxLifetime = TimeUnit.MINUTES.toMillis(30); this.maxLifetime = TimeUnit.MINUTES.toMillis(60);
this.connectionTimeout = TimeUnit.SECONDS.toMillis(30); this.connectionTimeout = TimeUnit.SECONDS.toMillis(15);
this.validationTimeout = TimeUnit.SECONDS.toMillis(5); this.validationTimeout = TimeUnit.SECONDS.toMillis(15);
this.idleTimeout = TimeUnit.MINUTES.toMillis(1); this.idleTimeout = TimeUnit.MINUTES.toMillis(1);
this.initializationFailTimeout = -1; this.initializationFailTimeout = -1;
this.isAutoCommit = true; // JDBC convention this.isAutoCommit = true; // JDBC convention
this.aliveBypassWindowMs = TimeUnit.MILLISECONDS.toMillis(500); this.aliveBypassWindowMs = TimeUnit.MILLISECONDS.toMillis(500);
this.housekeepingPeriodMs = TimeUnit.SECONDS.toMillis(30); this.housekeepingPeriodMs = TimeUnit.SECONDS.toMillis(60);
} }
/** /**
@ -639,13 +639,11 @@ public class PoolConfig {
} }
} }
if (connectionTimeout < 250) { if (connectionTimeout < 250) {
logger.log(Level.WARNING, "connectionTimeout is less than 250ms, setting to ms: " + logger.log(Level.WARNING, poolName + " connectionTimeout is less than 250ms, setting to " + TimeUnit.MILLISECONDS.toMillis(250) + " ms");
poolName + " " + TimeUnit.MILLISECONDS.toMillis(250));
connectionTimeout = TimeUnit.MILLISECONDS.toMillis(250); connectionTimeout = TimeUnit.MILLISECONDS.toMillis(250);
} }
if (validationTimeout < 250) { if (validationTimeout < 250) {
logger.log(Level.WARNING, "validationTimeout is less than 250ms, setting to ms" + logger.log(Level.WARNING, poolName + " validationTimeout is less than 250ms, setting to ms" + TimeUnit.MILLISECONDS.toMillis(250) + " ms");
poolName + " " + TimeUnit.MILLISECONDS.toMillis(250));
validationTimeout = TimeUnit.MILLISECONDS.toMillis(250); validationTimeout = TimeUnit.MILLISECONDS.toMillis(250);
} }
if (maxPoolSize < 1) { if (maxPoolSize < 1) {
@ -655,14 +653,13 @@ public class PoolConfig {
minIdle = maxPoolSize; minIdle = maxPoolSize;
} }
if (idleTimeout + TimeUnit.SECONDS.toMillis(1) > maxLifetime && maxLifetime > 0 && minIdle < maxPoolSize) { if (idleTimeout + TimeUnit.SECONDS.toMillis(1) > maxLifetime && maxLifetime > 0 && minIdle < maxPoolSize) {
logger.log(Level.WARNING, "idleTimeout is close to or more than maxLifetime, disabling it:" + poolName); logger.log(Level.WARNING, poolName + " idleTimeout is close to or more than maxLifetime, disabling it");
idleTimeout = 0; idleTimeout = 0;
} else if (idleTimeout != 0 && idleTimeout < TimeUnit.SECONDS.toMillis(10) && minIdle < maxPoolSize) { } else if (idleTimeout != 0 && idleTimeout < TimeUnit.SECONDS.toMillis(10) && minIdle < maxPoolSize) {
logger.log(Level.WARNING, "idleTimeout is less than 10s, setting to default ms: " + logger.log(Level.WARNING, poolName + " idleTimeout is less than 10s, setting to default " + TimeUnit.SECONDS.toMillis(10) + " ms");
poolName + " " + TimeUnit.SECONDS.toMillis(10));
idleTimeout = TimeUnit.SECONDS.toMillis(10); idleTimeout = TimeUnit.SECONDS.toMillis(10);
} else if (idleTimeout != TimeUnit.SECONDS.toMillis(10) && idleTimeout != 0 && minIdle == maxPoolSize) { } else if (idleTimeout != TimeUnit.SECONDS.toMillis(10) && idleTimeout != 0 && minIdle == maxPoolSize) {
logger.log(Level.WARNING, "idleTimeout has been set but has no effect because the pool is operating as a fixed size pool: " + poolName); logger.log(Level.WARNING, poolName + " idleTimeout has been set but has no effect because the pool is operating as a fixed size pool");
} }
} }

View file

@ -125,7 +125,7 @@ public interface Database extends Supplier<Database> {
* Probe the underlying connection. * Probe the underlying connection.
* @return true if database has executed a probe query successfully, false if not * @return true if database has executed a probe query successfully, false if not
*/ */
boolean probe() throws SQLException; boolean probe(int seconds) throws SQLException;
Options options(); Options options();
@ -280,5 +280,5 @@ public interface Database extends Supplier<Database> {
* @param consumer a consumer for the queue table data column or null * @param consumer a consumer for the queue table data column or null
* @throws SQLException if rollback fails * @throws SQLException if rollback fails
*/ */
public void consumeQueue(String table, String channel, int limit, Consumer<String> consumer) throws SQLException; void consumeQueue(String table, String channel, int limit, Consumer<String> consumer) throws SQLException;
} }

View file

@ -140,9 +140,8 @@ public class DatabaseImpl implements Database {
} }
@Override @Override
public boolean probe() throws SQLException { public boolean probe(int seconds) throws SQLException {
// use a standard timeout of 5 seconds return connection != null && connection.isValid(seconds);
return connection != null && connection.isValid(5);
} }
@Override @Override

View file

@ -320,15 +320,15 @@ public final class DatabaseProvider implements Supplier<Database>, Closeable {
return database; return database;
} }
public boolean check() { public boolean check(int seconds) {
Metric metric = new Metric(logger.isLoggable(Level.FINE)); Metric metric = new Metric(logger.isLoggable(Level.FINE));
boolean probe = false; boolean probe = false;
try { try {
if (database != null) { if (database != null) {
probe = database.probe(); probe = database.probe(seconds);
} else { } else {
get(); get();
probe = database.probe(); probe = database.probe(seconds);
} }
} catch (SQLException | RuntimeException e) { } catch (SQLException | RuntimeException e) {
metric.checkpoint("fail", e.getMessage()); metric.checkpoint("fail", e.getMessage());

View file

@ -20,7 +20,7 @@ public class OptionsDefault implements Options {
@Override @Override
public int timeoutSeconds() { public int timeoutSeconds() {
return 5; return 15;
} }
@Override @Override

View file

@ -77,7 +77,7 @@ public abstract class CommonTest {
dbp = createDatabaseProvider(new OptionsOverride() { dbp = createDatabaseProvider(new OptionsOverride() {
}); });
db = dbp.get(); db = dbp.get();
if (!db.probe()) { if (!db.probe(15)) {
fail(); fail();
} }
db.dropTableQuietly(TEST_TABLE_NAME); db.dropTableQuietly(TEST_TABLE_NAME);