From 6865443ddf8dfbde869fcac8fcdb669990d3709d Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Wed, 5 Mar 2014 13:46:43 +0900 Subject: [PATCH] Make HikariDataSource inherit from HikariConfig for convenience. This is useful for Spring configuration, for example, because properties can be set directly on the datasource rather than constructing a separate object. --- .../java/com/zaxxer/hikari/HikariConfig.java | 2 +- .../com/zaxxer/hikari/HikariDataSource.java | 46 +++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index c19e8779..ee44fda7 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory; import com.zaxxer.hikari.proxy.JavassistProxyFactory; import com.zaxxer.hikari.util.PropertyBeanSetter; -public final class HikariConfig implements HikariConfigMBean +public class HikariConfig implements HikariConfigMBean { private static final long ACQUIRE_RETRY_DELAY = 750L; private static final long CONNECTION_TIMEOUT = 5000L; diff --git a/src/main/java/com/zaxxer/hikari/HikariDataSource.java b/src/main/java/com/zaxxer/hikari/HikariDataSource.java index acc148ef..830d4936 100644 --- a/src/main/java/com/zaxxer/hikari/HikariDataSource.java +++ b/src/main/java/com/zaxxer/hikari/HikariDataSource.java @@ -31,15 +31,25 @@ import org.slf4j.LoggerFactory; * * @author Brett Wooldridge */ -public class HikariDataSource implements DataSource +public class HikariDataSource extends HikariConfig implements DataSource { private static final Logger LOGGER = LoggerFactory.getLogger(HikariDataSource.class); - private int loginTimeout; private volatile boolean isShutdown; + private int loginTimeout; + + HikariPool fastPathPool; + volatile HikariPool pool; - // Package scope for testing - HikariPool pool; + /** + * Default constructor. Setters be used to configure the pool. Using + * this constructor vs. {@link #HikariDataSource(HikariConfig)} will + * result in {@link #getConnection()} performance that is slightly lower + * due to lazy initialization checks. + */ + public HikariDataSource() + { + } /** * Construct a HikariDataSource with the specified configuration. @@ -48,24 +58,42 @@ public class HikariDataSource implements DataSource */ public HikariDataSource(HikariConfig configuration) { - pool = new HikariPool(configuration); + pool = fastPathPool = new HikariPool(configuration); } /** {@inheritDoc} */ public Connection getConnection() throws SQLException { - if (!isShutdown) + if (isShutdown) + { + throw new IllegalStateException("The datasource has been shutdown."); + } + + if (fastPathPool != null) { - return pool.getConnection(); + return fastPathPool.getConnection(); } - throw new IllegalStateException("The datasource has been shutdown."); + HikariPool result = pool; + if (result == null) + { + synchronized (this) + { + result = pool; + if (result == null) + { + pool = result = new HikariPool(this); + } + } + } + + return result.getConnection(); } /** {@inheritDoc} */ public Connection getConnection(String username, String password) throws SQLException { - LOGGER.warn("getConnection() with username and password is not supported"); + LOGGER.warn("getConnection() with username and password is not supported, calling getConnection() instead"); return getConnection(); }