From 8ec2953efd1c45969168f0afdd47a41e8146b12d Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Thu, 20 Mar 2014 19:21:17 +0900 Subject: [PATCH] Add username and password properties as well as cleanup validation (not complete). --- .../java/com/zaxxer/hikari/HikariConfig.java | 117 ++++++++++++------ 1 file changed, 81 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index e1edb6e6..02e60aef 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -31,7 +31,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.zaxxer.hikari.proxy.JavassistProxyFactory; -import com.zaxxer.hikari.util.DriverDataSource; import com.zaxxer.hikari.util.PropertyBeanSetter; public class HikariConfig implements HikariConfigMBean @@ -62,8 +61,10 @@ public class HikariConfig implements HikariConfigMBean private String connectionTestQuery; private String dataSourceClassName; private String jdbcUrl; + private String password; private String poolName; private String transactionIsolationName; + private String username; private boolean isAutoCommit; private boolean isReadOnly; private boolean isInitializationFailFast; @@ -529,6 +530,24 @@ public class HikariConfig implements HikariConfigMBean this.maxPoolSize = maxPoolSize; } + /** + * Get the default password to use for DataSource.getConnection(username, password) calls. + * @return the password + */ + public String getPassword() + { + return password; + } + + /** + * Set the default password to use for DataSource.getConnection(username, password) calls. + * @param password the password + */ + public void setPassword(String password) + { + this.password = password; + } + /** {@inheritDoc} */ @Override public String getPoolName() @@ -564,10 +583,32 @@ public class HikariConfig implements HikariConfigMBean this.transactionIsolationName = isolationLevel; } + /** + * Get the default username used for DataSource.getConnection(username, password) calls. + * + * @return the username + */ + public String getUsername() + { + return username; + } + + /** + * Set the default username used for DataSource.getConnection(username, password) calls. + * + * @param username the username + */ + public void setUsername(String username) + { + this.username = username; + } + public void validate() { Logger logger = LoggerFactory.getLogger(getClass()); + validateNumerics(); + if (connectionCustomizerClassName != null && connectionCustomizer == null) { try @@ -582,35 +623,59 @@ public class HikariConfig implements HikariConfigMBean } } - if (connectionTimeout == Integer.MAX_VALUE) - { - logger.warn("No connection wait timeout is set, this might cause an infinite wait."); - } - else if (connectionTimeout < TimeUnit.MILLISECONDS.toMillis(100)) - { - logger.warn("connectionTimeout is less than 100ms, did you specify the wrong time unit? Using default instead."); - connectionTimeout = CONNECTION_TIMEOUT; - } - if (jdbcUrl != null) { logger.info("Really, a JDBC URL? It's time to party like it's 1999!"); - dataSource = new DriverDataSource(jdbcUrl, dataSourceProperties); } else if (dataSource == null && dataSourceClassName == null) { - logger.error("one of either dataSource or dataSourceClassName must be specified"); - throw new IllegalStateException("one of either dataSource or dataSourceClassName must be specified"); + logger.error("one of either dataSource, dataSourceClassName, or jdbcUrl and driverClassName must be specified"); + throw new IllegalArgumentException("one of either dataSource or dataSourceClassName must be specified"); } else if (dataSource != null && dataSourceClassName != null) { logger.warn("both dataSource and dataSourceClassName are specified, ignoring dataSourceClassName"); } + if (!isJdbc4connectionTest && connectionTestQuery == null) + { + logger.error("Either jdbc4ConnectionTest must be enabled or a connectionTestQuery must be specified."); + throw new IllegalStateException("Either jdbc4ConnectionTest must be enabled or a connectionTestQuery must be specified."); + } + + if (transactionIsolationName != null) + { + try + { + Field field = Connection.class.getField(transactionIsolationName); + int level = field.getInt(null); + this.transactionIsolation = level; + } + catch (Exception e) + { + throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName); + } + } + } + + private void validateNumerics() + { + Logger logger = LoggerFactory.getLogger(getClass()); + + if (connectionTimeout == Integer.MAX_VALUE) + { + logger.warn("No connection wait timeout is set, this might cause an infinite wait."); + } + else if (connectionTimeout < TimeUnit.MILLISECONDS.toMillis(100)) + { + logger.warn("connectionTimeout is less than 100ms, did you specify the wrong time unit? Using default instead."); + connectionTimeout = CONNECTION_TIMEOUT; + } + if (idleTimeout < 0) { logger.error("idleTimeout cannot be negative."); - throw new IllegalStateException("idleTimeout cannot be negative."); + throw new IllegalArgumentException("idleTimeout cannot be negative."); } else if (idleTimeout < TimeUnit.SECONDS.toMillis(30) && idleTimeout != 0) { @@ -618,12 +683,6 @@ public class HikariConfig implements HikariConfigMBean idleTimeout = IDLE_TIMEOUT; } - if (!isJdbc4connectionTest && connectionTestQuery == null) - { - logger.error("Either jdbc4ConnectionTest must be enabled or a connectionTestQuery must be specified."); - throw new IllegalStateException("Either jdbc4ConnectionTest must be enabled or a connectionTestQuery must be specified."); - } - if (leakDetectionThreshold != 0 && leakDetectionThreshold < TimeUnit.SECONDS.toMillis(10)) { logger.warn("leakDetectionThreshold is less than 10000ms, did you specify the wrong time unit? Disabling leak detection."); @@ -639,7 +698,7 @@ public class HikariConfig implements HikariConfigMBean if (maxLifetime < 0) { logger.error("maxLifetime cannot be negative."); - throw new IllegalStateException("maxLifetime cannot be negative."); + throw new IllegalArgumentException("maxLifetime cannot be negative."); } else if (maxLifetime < TimeUnit.SECONDS.toMillis(120) && maxLifetime != 0) { @@ -658,20 +717,6 @@ public class HikariConfig implements HikariConfigMBean connectionTimeout = TimeUnit.SECONDS.toMillis(acquireRetries + 1); } } - - if (transactionIsolationName != null) - { - try - { - Field field = Connection.class.getField(transactionIsolationName); - int level = field.getInt(null); - this.transactionIsolation = level; - } - catch (Exception e) - { - throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName); - } - } } IConnectionCustomizer getConnectionCustomizer()