From b5c340eee30c21c5446a87f229babd15a7494223 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Wed, 11 Apr 2018 01:44:13 +0900 Subject: [PATCH] Permit changing the catalog via the HikariConfigMXBean --- .../java/com/zaxxer/hikari/HikariConfig.java | 39 ++++++++----------- .../com/zaxxer/hikari/HikariConfigMXBean.java | 26 ++++++++++--- .../com/zaxxer/hikari/pool/HikariPool.java | 3 +- .../java/com/zaxxer/hikari/pool/PoolBase.java | 4 +- 4 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index a037bbe1..2861df53 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -61,6 +61,7 @@ public class HikariConfig implements HikariConfigMXBean // Properties changeable at runtime through the HikariConfigMXBean // + private volatile String catalog; private volatile long connectionTimeout; private volatile long validationTimeout; private volatile long idleTimeout; @@ -74,7 +75,6 @@ public class HikariConfig implements HikariConfigMXBean // Properties NOT changeable at runtime // private long initializationFailTimeout; - private String catalog; private String connectionInitSql; private String connectionTestQuery; private String dataSourceClassName; @@ -152,6 +152,21 @@ public class HikariConfig implements HikariConfigMXBean // HikariConfigMXBean methods // *********************************************************************** + /** {@inheritDoc} */ + @Override + public String getCatalog() + { + return catalog; + } + + /** {@inheritDoc} */ + @Override + public void setCatalog(String catalog) + { + this.catalog = catalog; + } + + /** {@inheritDoc} */ @Override public long getConnectionTimeout() @@ -315,28 +330,6 @@ public class HikariConfig implements HikariConfigMXBean // All other configuration methods // *********************************************************************** - /** - * Get the default catalog name to be set on connections. - * - * @return the default catalog name - */ - public String getCatalog() - { - return catalog; - } - - /** - * Set the default catalog name to be set on connections. - * - * @param catalog the catalog name, or null - */ - public void setCatalog(String catalog) - { - if (sealed) throw new IllegalStateException("The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes."); - - this.catalog = catalog; - } - /** * Get the SQL query to be executed to test the validity of connections. * diff --git a/src/main/java/com/zaxxer/hikari/HikariConfigMXBean.java b/src/main/java/com/zaxxer/hikari/HikariConfigMXBean.java index 474152b3..2e510d53 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfigMXBean.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfigMXBean.java @@ -24,7 +24,7 @@ package com.zaxxer.hikari; public interface HikariConfigMXBean { /** - * Get the maximum number of milliseconds that a client will wait for a connection from the pool. If this + * Get the maximum number of milliseconds that a client will wait for a connection from the pool. If this * time is exceeded without a connection becoming available, a SQLException will be thrown from * {@link javax.sql.DataSource#getConnection()}. * @@ -58,7 +58,7 @@ public interface HikariConfigMXBean void setValidationTimeout(long validationTimeoutMs); /** - * This property controls the maximum amount of time (in milliseconds) that a connection is allowed to sit + * This property controls the maximum amount of time (in milliseconds) that a connection is allowed to sit * idle in the pool. Whether a connection is retired as idle or not is subject to a maximum variation of +30 * seconds, and average variation of +15 seconds. A connection will never be retired as idle before this timeout. * A value of 0 means that idle connections are never removed from the pool. @@ -68,7 +68,7 @@ public interface HikariConfigMXBean long getIdleTimeout(); /** - * This property controls the maximum amount of time (in milliseconds) that a connection is allowed to sit + * This property controls the maximum amount of time (in milliseconds) that a connection is allowed to sit * idle in the pool. Whether a connection is retired as idle or not is subject to a maximum variation of +30 * seconds, and average variation of +15 seconds. A connection will never be retired as idle before this timeout. * A value of 0 means that idle connections are never removed from the pool. @@ -131,7 +131,7 @@ public interface HikariConfigMXBean /** * The property controls the maximum number of connections that HikariCP will keep in the pool, - * including both idle and in-use connections. + * including both idle and in-use connections. * * @return the maximum number of connections in the pool */ @@ -167,11 +167,27 @@ public interface HikariConfigMXBean */ void setUsername(String username); - + /** * The name of the connection pool. * * @return the name of the connection pool */ String getPoolName(); + + /** + * Get the default catalog name to be set on connections. + * + * @return the default catalog name + */ + String getCatalog(); + + /** + * Set the default catalog name to be set on connections. + *

+ * WARNING: THIS VALUE SHOULD ONLY BE CHANGED WHILE THE POOL IS SUSPENDED, AFTER CONNECTIONS HAVE BEEN EVICTED. + * + * @param catalog the catalog name, or null + */ + void setCatalog(String catalog); } diff --git a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java index 0b35fba6..6966e530 100644 --- a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java +++ b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java @@ -744,10 +744,11 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag public void run() { try { - // refresh timeouts in case they changed via MBean + // refresh values in case they changed via MBean connectionTimeout = config.getConnectionTimeout(); validationTimeout = config.getValidationTimeout(); leakTaskFactory.updateLeakDetectionThreshold(config.getLeakDetectionThreshold()); + catalog = (config.getCatalog() != null && !config.getCatalog().equals(catalog)) ? config.getCatalog() : catalog; final long idleTimeout = config.getIdleTimeout(); final long now = currentTime(); diff --git a/src/main/java/com/zaxxer/hikari/pool/PoolBase.java b/src/main/java/com/zaxxer/hikari/pool/PoolBase.java index 255687de..91477b78 100644 --- a/src/main/java/com/zaxxer/hikari/pool/PoolBase.java +++ b/src/main/java/com/zaxxer/hikari/pool/PoolBase.java @@ -55,7 +55,10 @@ abstract class PoolBase public final HikariConfig config; public IMetricsTrackerDelegate metricsTracker; + + protected volatile String catalog; protected final String poolName; + long connectionTimeout; long validationTimeout; @@ -72,7 +75,6 @@ abstract class PoolBase private Executor netTimeoutExecutor; private DataSource dataSource; - private final String catalog; private final String schema; private final boolean isReadOnly; private final boolean isAutoCommit;