From cfda5e022c3ca104d7748d199d1a76decc2c2fc9 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Fri, 25 Apr 2014 08:24:47 +0900 Subject: [PATCH] This fixes #65 as well as correcting OSGi manifest generation warnings by reorganizing the code and packages somewhat to ensure that the publicly exported com.zaxxer.hikari package does not expose any internal classes. --- pom.xml | 6 +- .../java/com/zaxxer/hikari/HikariConfig.java | 11 +- .../com/zaxxer/hikari/HikariDataSource.java | 25 ++--- .../metrics/CodaHaleMetricsTracker.java | 2 +- .../hikari/{ => pool}/HikariMBeanElf.java | 4 +- .../zaxxer/hikari/{ => pool}/HikariPool.java | 102 ++++++++++++------ .../zaxxer/hikari/proxy/ConnectionProxy.java | 2 +- .../com/zaxxer/hikari/proxy/ProxyFactory.java | 2 +- .../com/zaxxer/hikari/util/PoolUtilities.java | 7 ++ .../java/com/zaxxer/hikari/ExceptionTest.java | 24 ++--- .../java/com/zaxxer/hikari/RampUpDown.java | 6 +- .../java/com/zaxxer/hikari/StatementTest.java | 8 +- .../hikari/TestConnectionTimeoutRetry.java | 8 +- .../com/zaxxer/hikari/TestConnections.java | 46 ++++---- src/test/java/com/zaxxer/hikari/TestElf.java | 27 +++++ .../java/com/zaxxer/hikari/UnwrapTest.java | 2 +- 16 files changed, 174 insertions(+), 108 deletions(-) rename src/main/java/com/zaxxer/hikari/{ => pool}/HikariMBeanElf.java (97%) rename src/main/java/com/zaxxer/hikari/{ => pool}/HikariPool.java (92%) create mode 100644 src/test/java/com/zaxxer/hikari/TestElf.java diff --git a/pom.xml b/pom.xml index 19667fb1..fe749d4c 100644 --- a/pom.xml +++ b/pom.xml @@ -82,12 +82,14 @@ hibernate-core 4.3.0.Final provided + true com.codahale.metrics metrics-core 3.0.2 provided + true @@ -152,7 +154,8 @@ HikariCP - com.zaxxer.hikari + com.zaxxer.hikari,com.zaxxer.hikari.hibernate + com.zaxxer.hikari.* javassist.*, javax.management, @@ -160,6 +163,7 @@ javax.sql.rowset, javax.sql.rowset.serial, javax.sql.rowset.spi, + com.codahale.metrics;;resolution:=optional, org.slf4j, org.hibernate;resolution:=optional, org.hibernate.cfg;resolution:=optional, diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index d9d7a27b..741937c9 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -74,7 +74,6 @@ public class HikariConfig implements HikariConfigMBean private boolean isRegisterMbeans; private DataSource dataSource; private Properties dataSourceProperties; - private IConnectionCustomizer connectionCustomizer; private int transactionIsolation; static @@ -611,12 +610,11 @@ public class HikariConfig implements HikariConfigMBean validateNumerics(); - if (connectionCustomizerClassName != null && connectionCustomizer == null) + if (connectionCustomizerClassName != null) { try { - Class customizerClass = getClass().getClassLoader().loadClass(connectionCustomizerClassName); - connectionCustomizer = (IConnectionCustomizer) customizerClass.newInstance(); + getClass().getClassLoader().loadClass(connectionCustomizerClassName); } catch (Exception e) { @@ -722,11 +720,6 @@ public class HikariConfig implements HikariConfigMBean } } - IConnectionCustomizer getConnectionCustomizer() - { - return connectionCustomizer; - } - void copyState(HikariConfig other) { for (Field field : HikariConfig.class.getDeclaredFields()) diff --git a/src/main/java/com/zaxxer/hikari/HikariDataSource.java b/src/main/java/com/zaxxer/hikari/HikariDataSource.java index 747365df..94dcb1ca 100644 --- a/src/main/java/com/zaxxer/hikari/HikariDataSource.java +++ b/src/main/java/com/zaxxer/hikari/HikariDataSource.java @@ -24,6 +24,7 @@ import java.util.HashMap; import javax.sql.DataSource; +import com.zaxxer.hikari.pool.HikariPool; import com.zaxxer.hikari.proxy.IHikariConnectionProxy; import com.zaxxer.hikari.util.DriverDataSource; @@ -39,8 +40,8 @@ public class HikariDataSource extends HikariConfig implements DataSource private int loginTimeout; /* Package scopped for testing */ - final HikariPool fastPathPool; - volatile HikariPool pool; + private final HikariPool fastPathPool; + private volatile HikariPool pool; /** * Default constructor. Setters be used to configure the pool. Using @@ -131,16 +132,16 @@ public class HikariDataSource extends HikariConfig implements DataSource @Override public PrintWriter getLogWriter() throws SQLException { - return (pool.dataSource != null ? pool.dataSource.getLogWriter() : null); + return (pool.getDataSource() != null ? pool.getDataSource().getLogWriter() : null); } /** {@inheritDoc} */ @Override public void setLogWriter(PrintWriter out) throws SQLException { - if (pool.dataSource != null) + if (pool.getDataSource() != null) { - pool.dataSource.setLogWriter(out); + pool.getDataSource().setLogWriter(out); } } @@ -169,9 +170,9 @@ public class HikariDataSource extends HikariConfig implements DataSource @SuppressWarnings("unchecked") public T unwrap(Class iface) throws SQLException { - if (pool != null && iface.isInstance(pool.dataSource)) + if (pool != null && iface.isInstance(pool.getDataSource())) { - return (T) pool.dataSource; + return (T) pool.getDataSource(); } throw new SQLException("Wrapped connection is not an instance of " + iface); @@ -181,7 +182,7 @@ public class HikariDataSource extends HikariConfig implements DataSource @Override public boolean isWrapperFor(Class iface) throws SQLException { - return (pool != null & pool.dataSource.getClass().isAssignableFrom(iface)); + return (pool != null & pool.getDataSource().getClass().isAssignableFrom(iface)); } /** @@ -222,9 +223,9 @@ public class HikariDataSource extends HikariConfig implements DataSource if (pool != null) { pool.shutdown(); - if (pool.dataSource instanceof DriverDataSource) + if (pool.getDataSource() instanceof DriverDataSource) { - ((DriverDataSource) pool.dataSource).shutdown(); + ((DriverDataSource) pool.getDataSource()).shutdown(); } } @@ -233,9 +234,9 @@ public class HikariDataSource extends HikariConfig implements DataSource for (HikariPool hikariPool : multiPool.values()) { hikariPool.shutdown(); - if (hikariPool.dataSource instanceof DriverDataSource) + if (hikariPool.getDataSource() instanceof DriverDataSource) { - ((DriverDataSource) hikariPool.dataSource).shutdown(); + ((DriverDataSource) hikariPool.getDataSource()).shutdown(); } } } diff --git a/src/main/java/com/zaxxer/hikari/metrics/CodaHaleMetricsTracker.java b/src/main/java/com/zaxxer/hikari/metrics/CodaHaleMetricsTracker.java index 3bf21404..f0bcc75c 100644 --- a/src/main/java/com/zaxxer/hikari/metrics/CodaHaleMetricsTracker.java +++ b/src/main/java/com/zaxxer/hikari/metrics/CodaHaleMetricsTracker.java @@ -19,7 +19,7 @@ package com.zaxxer.hikari.metrics; import com.codahale.metrics.Histogram; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Timer; -import com.zaxxer.hikari.HikariPool; +import com.zaxxer.hikari.pool.HikariPool; public final class CodaHaleMetricsTracker extends MetricsTracker { diff --git a/src/main/java/com/zaxxer/hikari/HikariMBeanElf.java b/src/main/java/com/zaxxer/hikari/pool/HikariMBeanElf.java similarity index 97% rename from src/main/java/com/zaxxer/hikari/HikariMBeanElf.java rename to src/main/java/com/zaxxer/hikari/pool/HikariMBeanElf.java index d4181832..e4fe8cb6 100644 --- a/src/main/java/com/zaxxer/hikari/HikariMBeanElf.java +++ b/src/main/java/com/zaxxer/hikari/pool/HikariMBeanElf.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.zaxxer.hikari; +package com.zaxxer.hikari.pool; import java.lang.management.ManagementFactory; @@ -24,6 +24,8 @@ import javax.management.ObjectName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.zaxxer.hikari.HikariConfig; + /** * Helper class to register our MBeans. * diff --git a/src/main/java/com/zaxxer/hikari/HikariPool.java b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java similarity index 92% rename from src/main/java/com/zaxxer/hikari/HikariPool.java rename to src/main/java/com/zaxxer/hikari/pool/HikariPool.java index 513f2141..9ea36b38 100644 --- a/src/main/java/com/zaxxer/hikari/HikariPool.java +++ b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.zaxxer.hikari; +package com.zaxxer.hikari.pool; import java.sql.Connection; import java.sql.SQLException; @@ -32,6 +32,9 @@ import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariPoolMBean; +import com.zaxxer.hikari.IConnectionCustomizer; import com.zaxxer.hikari.metrics.CodaHaleMetricsTracker; import com.zaxxer.hikari.metrics.MetricsTracker; import com.zaxxer.hikari.metrics.MetricsTracker.Context; @@ -53,7 +56,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener { private static final Logger LOGGER = LoggerFactory.getLogger(HikariPool.class); - final DataSource dataSource; + private final DataSource dataSource; private final IConnectionCustomizer connectionCustomizer; private final HikariConfig configuration; @@ -78,7 +81,12 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener private int transactionIsolation; private boolean isDebug; - HikariPool(HikariConfig configuration) + /** + * Construct a HikariPool with the specified configuration. + * + * @param configuration a HikariConfig instance + */ + public HikariPool(HikariConfig configuration) { this(configuration, configuration.getUsername(), configuration.getPassword()); } @@ -87,8 +95,10 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener * Construct a HikariPool with the specified configuration. * * @param configuration a HikariConfig instance + * @param username authentication username + * @param password authentication password */ - HikariPool(HikariConfig configuration, String username, String password) + public HikariPool(HikariConfig configuration, String username, String password) { this.configuration = configuration; this.username = username; @@ -101,7 +111,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener this.lastConnectionFailure = new AtomicReference(); this.catalog = configuration.getCatalog(); - this.connectionCustomizer = configuration.getConnectionCustomizer(); + this.connectionCustomizer = initializeCustomizer(); this.isAutoCommit = configuration.isAutoCommit(); this.isIsolateInternalQueries = configuration.isIsolateInternalQueries(); this.isReadOnly = configuration.isReadOnly(); @@ -137,7 +147,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener * @return a java.sql.Connection instance * @throws SQLException thrown if a timeout occurs trying to obtain a connection */ - Connection getConnection() throws SQLException + public Connection getConnection() throws SQLException { final long start = System.currentTimeMillis(); final long maxLife = configuration.getMaxLifetime(); @@ -212,7 +222,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener return configuration.getPoolName(); } - void shutdown() + public void shutdown() { isShutdown = true; houseKeepingTimer.cancel(); @@ -229,6 +239,38 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener } } + public DataSource getDataSource() + { + return dataSource; + } + + + /** + * Permanently close a connection. + * + * @param connectionProxy the connection to actually close + */ + public void closeConnection(IHikariConnectionProxy connectionProxy) + { + try + { + int tc = totalConnections.decrementAndGet(); + if (tc < 0) + { + LOGGER.warn("Internal accounting inconsistency, totalConnections=" + tc, new Exception()); + } + connectionProxy.realClose(); + } + catch (SQLException e) + { + return; + } + finally + { + connectionBag.remove(connectionProxy); + } + } + // *********************************************************************** // IBagStateListener methods // *********************************************************************** @@ -443,32 +485,6 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener } } - /** - * Permanently close a connection. - * - * @param connectionProxy the connection to actually close - */ - void closeConnection(IHikariConnectionProxy connectionProxy) - { - try - { - int tc = totalConnections.decrementAndGet(); - if (tc < 0) - { - LOGGER.warn("Internal accounting inconsistency, totalConnections=" + tc, new Exception()); - } - connectionProxy.realClose(); - } - catch (SQLException e) - { - return; - } - finally - { - connectionBag.remove(connectionProxy); - } - } - private DataSource initializeDataSource() { String dsClassName = configuration.getDataSourceClassName(); @@ -476,8 +492,7 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener { try { - Class clazz = this.getClass().getClassLoader().loadClass(dsClassName); - DataSource dataSource = (DataSource) clazz.newInstance(); + DataSource dataSource = PoolUtilities.createInstance(dsClassName, DataSource.class); PropertyBeanSetter.setTargetFromProperties(dataSource, configuration.getDataSourceProperties()); return dataSource; } @@ -494,6 +509,23 @@ public final class HikariPool implements HikariPoolMBean, IBagStateListener return configuration.getDataSource(); } + private IConnectionCustomizer initializeCustomizer() + { + if (configuration.getConnectionCustomizerClassName() != null) + { + try + { + return PoolUtilities.createInstance(configuration.getConnectionCustomizerClassName(), IConnectionCustomizer.class); + } + catch (Exception e) + { + LOGGER.error("Connection customizer could not be created", e); + } + } + + return null; + } + private void logPoolState(String... prefix) { int total = totalConnections.get(); diff --git a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java index ef3904fd..b65899cd 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java @@ -30,7 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.zaxxer.hikari.HikariPool; +import com.zaxxer.hikari.pool.HikariPool; import com.zaxxer.hikari.util.FastStatementList; /** diff --git a/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java b/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java index d07f57e7..c7b0fdee 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java @@ -21,7 +21,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; -import com.zaxxer.hikari.HikariPool; +import com.zaxxer.hikari.pool.HikariPool; /** * A factory class that produces proxies around instances of the standard diff --git a/src/main/java/com/zaxxer/hikari/util/PoolUtilities.java b/src/main/java/com/zaxxer/hikari/util/PoolUtilities.java index d28cf2e7..bd3845eb 100644 --- a/src/main/java/com/zaxxer/hikari/util/PoolUtilities.java +++ b/src/main/java/com/zaxxer/hikari/util/PoolUtilities.java @@ -58,6 +58,13 @@ public final class PoolUtilities } } + @SuppressWarnings("unchecked") + public static T createInstance(String className, Class clazz) throws Exception + { + Class loaded = PoolUtilities.class.getClassLoader().loadClass(className); + return (T) loaded.newInstance(); + } + public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName) { ThreadFactory threadFactory = new ThreadFactory() { diff --git a/src/test/java/com/zaxxer/hikari/ExceptionTest.java b/src/test/java/com/zaxxer/hikari/ExceptionTest.java index b34afc89..5242a76f 100644 --- a/src/test/java/com/zaxxer/hikari/ExceptionTest.java +++ b/src/test/java/com/zaxxer/hikari/ExceptionTest.java @@ -36,14 +36,14 @@ public class ExceptionTest @Test public void testException1() throws SQLException { - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection = ds.getConnection(); Assert.assertNotNull(connection); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?"); Assert.assertNotNull(statement); @@ -63,8 +63,8 @@ public class ExceptionTest connection.close(); - Assert.assertSame("Totals connections not as expected", 0, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 0, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); } @Test @@ -99,14 +99,14 @@ public class ExceptionTest HikariDataSource ds = new HikariDataSource(config); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection = ds.getConnection(); Assert.assertNotNull(connection); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); connection.close(); @@ -120,8 +120,8 @@ public class ExceptionTest Assert.assertSame("Connection is closed", e.getMessage()); } - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); } } diff --git a/src/test/java/com/zaxxer/hikari/RampUpDown.java b/src/test/java/com/zaxxer/hikari/RampUpDown.java index 03cfbb33..7d475076 100644 --- a/src/test/java/com/zaxxer/hikari/RampUpDown.java +++ b/src/test/java/com/zaxxer/hikari/RampUpDown.java @@ -23,7 +23,7 @@ public class RampUpDown HikariDataSource ds = new HikariDataSource(config); ds.setIdleTimeout(1000); - Assert.assertSame("Totals connections not as expected", 5, ds.pool.getTotalConnections()); + Assert.assertSame("Totals connections not as expected", 5, TestElf.getPool(ds).getTotalConnections()); Connection[] connections = new Connection[ds.getMaximumPoolSize()]; for (int i = 0; i < connections.length; i++) @@ -31,7 +31,7 @@ public class RampUpDown connections[i] = ds.getConnection(); } - Assert.assertSame("Totals connections not as expected", 60, ds.pool.getTotalConnections()); + Assert.assertSame("Totals connections not as expected", 60, TestElf.getPool(ds).getTotalConnections()); for (Connection connection : connections) { @@ -40,7 +40,7 @@ public class RampUpDown Thread.sleep(2500); - Assert.assertSame("Totals connections not as expected", 5, ds.pool.getTotalConnections()); + Assert.assertSame("Totals connections not as expected", 5, TestElf.getPool(ds).getTotalConnections()); ds.shutdown(); } diff --git a/src/test/java/com/zaxxer/hikari/StatementTest.java b/src/test/java/com/zaxxer/hikari/StatementTest.java index d35ee984..41deb233 100644 --- a/src/test/java/com/zaxxer/hikari/StatementTest.java +++ b/src/test/java/com/zaxxer/hikari/StatementTest.java @@ -21,14 +21,14 @@ public class StatementTest HikariDataSource ds = new HikariDataSource(config); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection = ds.getConnection(); Assert.assertNotNull(connection); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); Statement statement = connection.createStatement(); Assert.assertNotNull(statement); diff --git a/src/test/java/com/zaxxer/hikari/TestConnectionTimeoutRetry.java b/src/test/java/com/zaxxer/hikari/TestConnectionTimeoutRetry.java index d1c97a98..8bcdfec3 100644 --- a/src/test/java/com/zaxxer/hikari/TestConnectionTimeoutRetry.java +++ b/src/test/java/com/zaxxer/hikari/TestConnectionTimeoutRetry.java @@ -253,8 +253,8 @@ public class TestConnectionTimeoutRetry Thread.sleep(1000); - Assert.assertSame("Totals connections not as expected", 10, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 3, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 10, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 3, TestElf.getPool(ds).getIdleConnections()); connection1.close(); connection2.close(); @@ -264,7 +264,7 @@ public class TestConnectionTimeoutRetry connection6.close(); connection7.close(); - Assert.assertSame("Totals connections not as expected", 10, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 10, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 10, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 10, TestElf.getPool(ds).getIdleConnections()); } } diff --git a/src/test/java/com/zaxxer/hikari/TestConnections.java b/src/test/java/com/zaxxer/hikari/TestConnections.java index 3f3e03dd..8691b8a1 100644 --- a/src/test/java/com/zaxxer/hikari/TestConnections.java +++ b/src/test/java/com/zaxxer/hikari/TestConnections.java @@ -49,14 +49,14 @@ public class TestConnections HikariDataSource ds = new HikariDataSource(config); try { - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection = ds.getConnection(); Assert.assertNotNull(connection); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); PreparedStatement statement = connection.prepareStatement("SELECT * FROM device WHERE device_id=?"); Assert.assertNotNull(statement); @@ -72,8 +72,8 @@ public class TestConnections statement.close(); connection.close(); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); } finally { @@ -101,22 +101,22 @@ public class TestConnections ds.setMaxLifetime(700); - Assert.assertSame("Total connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Total connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection = ds.getConnection(); Assert.assertNotNull(connection); - Assert.assertSame("Second total connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Second idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Second total connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Second idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); connection.close(); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection2 = ds.getConnection(); Assert.assertSame("Expected the same connection", connection, connection2); - Assert.assertSame("Second total connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Second idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Second total connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Second idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); connection2.close(); Thread.sleep(2000); @@ -126,8 +126,8 @@ public class TestConnections connection2.close(); - Assert.assertSame("Post total connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Post idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Post total connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Post idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); } finally { @@ -172,15 +172,15 @@ public class TestConnections HikariDataSource ds = new HikariDataSource(config); try { - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); // This will take the pool down to zero Connection connection = ds.getConnection(); Assert.assertNotNull(connection); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?"); Assert.assertNotNull(statement); @@ -201,15 +201,15 @@ public class TestConnections // The connection will be ejected from the pool here connection.close(); - Assert.assertSame("Totals connections not as expected", 0, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 0, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 0, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 0, TestElf.getPool(ds).getIdleConnections()); // This will cause a backfill connection = ds.getConnection(); connection.close(); - Assert.assertSame("Totals connections not as expected", 1, ds.pool.getTotalConnections()); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Totals connections not as expected", 1, TestElf.getPool(ds).getTotalConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); } finally { diff --git a/src/test/java/com/zaxxer/hikari/TestElf.java b/src/test/java/com/zaxxer/hikari/TestElf.java new file mode 100644 index 00000000..c1af0be1 --- /dev/null +++ b/src/test/java/com/zaxxer/hikari/TestElf.java @@ -0,0 +1,27 @@ +package com.zaxxer.hikari; + +import java.lang.reflect.Field; + +import com.zaxxer.hikari.pool.HikariPool; + +public final class TestElf +{ + private TestElf() + { + // default constructor + } + + public static HikariPool getPool(HikariDataSource ds) + { + try + { + Field field = ds.getClass().getDeclaredField("pool"); + field.setAccessible(true); + return (HikariPool) field.get(ds); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } +} diff --git a/src/test/java/com/zaxxer/hikari/UnwrapTest.java b/src/test/java/com/zaxxer/hikari/UnwrapTest.java index 914a9a09..ae8f6829 100644 --- a/src/test/java/com/zaxxer/hikari/UnwrapTest.java +++ b/src/test/java/com/zaxxer/hikari/UnwrapTest.java @@ -42,7 +42,7 @@ public class UnwrapTest HikariDataSource ds = new HikariDataSource(config); - Assert.assertSame("Idle connections not as expected", 1, ds.pool.getIdleConnections()); + Assert.assertSame("Idle connections not as expected", 1, TestElf.getPool(ds).getIdleConnections()); Connection connection = ds.getConnection(); Assert.assertNotNull(connection);