Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettwooldridge committed Jun 1, 2015
1 parent f227cbe commit ef57e11
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
18 changes: 8 additions & 10 deletions src/main/java/com/zaxxer/hikari/pool/HikariPool.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class HikariPool implements HikariPoolMBean, IBagStateListener


private final LeakTask leakTask; private final LeakTask leakTask;
private final DataSource dataSource; private final DataSource dataSource;
private final GlobalPoolLock suspendResumeLock; private final SuspendResumeLock suspendResumeLock;
private final AtomicReference<Throwable> lastConnectionFailure; private final AtomicReference<Throwable> lastConnectionFailure;


private final String username; private final String username;
Expand All @@ -124,24 +124,23 @@ public HikariPool(HikariConfig config)
this.password = config.getPassword(); this.password = config.getPassword();


this.poolUtils = new PoolUtilities(config); this.poolUtils = new PoolUtilities(config);
this.dataSource = poolUtils.initializeDataSource(config.getDataSourceClassName(), config.getDataSource(), config.getDataSourceProperties(), config.getDriverClassName(), config.getJdbcUrl(), username, password); this.dataSource = poolUtils.initializeDataSource();


this.connectionBag = new ConcurrentBag<>(this); this.connectionBag = new ConcurrentBag<>(this);
this.totalConnections = new AtomicInteger(); this.totalConnections = new AtomicInteger();
this.connectionTimeout = config.getConnectionTimeout(); this.connectionTimeout = config.getConnectionTimeout();
this.validationTimeout = config.getValidationTimeout(); this.validationTimeout = config.getValidationTimeout();
this.lastConnectionFailure = new AtomicReference<>(); this.lastConnectionFailure = new AtomicReference<>();


this.catalog = config.getCatalog();
this.isReadOnly = config.isReadOnly(); this.isReadOnly = config.isReadOnly();
this.isAutoCommit = config.isAutoCommit(); this.isAutoCommit = config.isAutoCommit();
this.isUseJdbc4Validation = config.getConnectionTestQuery() == null;
this.isIsolateInternalQueries = config.isIsolateInternalQueries();
this.transactionIsolation = getTransactionIsolation(config.getTransactionIsolation());


this.suspendResumeLock = config.isAllowPoolSuspension() ? new GlobalPoolLock(true) : GlobalPoolLock.FAUX_LOCK; this.suspendResumeLock = config.isAllowPoolSuspension() ? new SuspendResumeLock(true) : SuspendResumeLock.FAUX_LOCK;


this.catalog = config.getCatalog();
this.transactionIsolation = getTransactionIsolation(config.getTransactionIsolation());
this.isIsolateInternalQueries = config.isIsolateInternalQueries();
this.isUseJdbc4Validation = config.getConnectionTestQuery() == null;

setMetricRegistry(config.getMetricRegistry()); setMetricRegistry(config.getMetricRegistry());
setHealthCheckRegistry(config.getHealthCheckRegistry()); setHealthCheckRegistry(config.getHealthCheckRegistry());


Expand All @@ -160,7 +159,6 @@ public HikariPool(HikariConfig config)
} }
this.leakTask = (config.getLeakDetectionThreshold() == 0) ? LeakTask.NO_LEAK : new LeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService); this.leakTask = (config.getLeakDetectionThreshold() == 0) ? LeakTask.NO_LEAK : new LeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService);


poolUtils.setLoginTimeout(dataSource, connectionTimeout);
registerMBeans(config, this); registerMBeans(config, this);


PropertyBeanSetter.flushCaches(); // prevent classloader leak PropertyBeanSetter.flushCaches(); // prevent classloader leak
Expand Down Expand Up @@ -434,7 +432,7 @@ public void softEvictConnections()
@Override @Override
public final synchronized void suspendPool() public final synchronized void suspendPool()
{ {
if (suspendResumeLock == GlobalPoolLock.FAUX_LOCK) { if (suspendResumeLock == SuspendResumeLock.FAUX_LOCK) {
throw new IllegalStateException("Pool " + configuration.getPoolName() + " is not suspendable"); throw new IllegalStateException("Pool " + configuration.getPoolName() + " is not suspendable");
} }
else if (poolState != POOL_SUSPENDED) { else if (poolState != POOL_SUSPENDED) {
Expand Down
58 changes: 32 additions & 26 deletions src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public final class PoolUtilities


private Executor netTimeoutExecutor; private Executor netTimeoutExecutor;


private final HikariConfig config;
private final String poolName; private final String poolName;
private volatile boolean isValidChecked; private volatile boolean isValidChecked;
private volatile boolean isValidSupported; private volatile boolean isValidSupported;
private boolean isNetworkTimeoutSupported; private boolean isNetworkTimeoutSupported;
private boolean isQueryTimeoutSupported; private boolean isQueryTimeoutSupported;




public PoolUtilities(final HikariConfig configuration) public PoolUtilities(final HikariConfig configuration)
{ {
this.config = configuration;
this.poolName = configuration.getPoolName(); this.poolName = configuration.getPoolName();
this.isValidSupported = true; this.isValidSupported = true;
this.isNetworkTimeoutSupported = true; this.isNetworkTimeoutSupported = true;
Expand Down Expand Up @@ -92,17 +96,18 @@ public void executeSql(final Connection connection, final String sql, final bool
/** /**
* Create/initialize the underlying DataSource. * Create/initialize the underlying DataSource.
* *
* @param dsClassName a DataSource class name (optional)
* @param dataSource a DataSource instance (optional)
* @param dataSourceProperties a Properties instance of DataSource properties
* @param driverClassName the JDBC driver class name (optional)
* @param jdbcUrl a JDBC connection URL (optional)
* @param username a username (optional)
* @param password a password (optional)
* @return a DataSource instance * @return a DataSource instance
*/ */
public DataSource initializeDataSource(final String dsClassName, DataSource dataSource, final Properties dataSourceProperties, final String driverClassName, final String jdbcUrl, final String username, final String password) public DataSource initializeDataSource()
{ {
final String jdbcUrl = config.getJdbcUrl();
final String username = config.getUsername();
final String password = config.getPassword();
final String dsClassName = config.getDataSourceClassName();
final String driverClassName = config.getDriverClassName();
final Properties dataSourceProperties = config.getDataSourceProperties();

DataSource dataSource = config.getDataSource();
if (dsClassName != null && dataSource == null) { if (dsClassName != null && dataSource == null) {
dataSource = createInstance(dsClassName, DataSource.class); dataSource = createInstance(dsClassName, DataSource.class);
PropertyBeanSetter.setTargetFromProperties(dataSource, dataSourceProperties); PropertyBeanSetter.setTargetFromProperties(dataSource, dataSourceProperties);
Expand All @@ -112,6 +117,7 @@ else if (jdbcUrl != null && dataSource == null) {
} }


if (dataSource != null) { if (dataSource != null) {
setLoginTimeout(dataSource, config.getConnectionTimeout());
createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl); createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl);
} }


Expand Down Expand Up @@ -224,24 +230,6 @@ public void setNetworkTimeout(final Connection connection, final long timeoutMs)
} }
} }


/**
* Set the loginTimeout on the specified DataSource.
*
* @param dataSource the DataSource
* @param connectionTimeout the timeout in milliseconds
*/
public void setLoginTimeout(final DataSource dataSource, final long connectionTimeout)
{
if (connectionTimeout != Integer.MAX_VALUE) {
try {
dataSource.setLoginTimeout((int) TimeUnit.MILLISECONDS.toSeconds(Math.max(1000L, connectionTimeout)));
}
catch (SQLException e) {
LOGGER.warn("Unable to set DataSource login timeout", e);
}
}
}

// Temporary hack for MySQL issue: http://bugs.mysql.com/bug.php?id=75615 // Temporary hack for MySQL issue: http://bugs.mysql.com/bug.php?id=75615
private void createNetworkTimeoutExecutor(final DataSource dataSource, final String dsClassName, final String jdbcUrl) private void createNetworkTimeoutExecutor(final DataSource dataSource, final String dsClassName, final String jdbcUrl)
{ {
Expand Down Expand Up @@ -270,4 +258,22 @@ public void execute(Runnable command)
} }
} }
} }

/**
* Set the loginTimeout on the specified DataSource.
*
* @param dataSource the DataSource
* @param connectionTimeout the timeout in milliseconds
*/
private void setLoginTimeout(final DataSource dataSource, final long connectionTimeout)
{
if (connectionTimeout != Integer.MAX_VALUE) {
try {
dataSource.setLoginTimeout((int) TimeUnit.MILLISECONDS.toSeconds(Math.max(1000L, connectionTimeout)));
}
catch (SQLException e) {
LOGGER.warn("Unable to set DataSource login timeout", e);
}
}
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
* *
* @author Brett Wooldridge * @author Brett Wooldridge
*/ */
class GlobalPoolLock class SuspendResumeLock
{ {
static final GlobalPoolLock FAUX_LOCK = new GlobalPoolLock(false) { static final SuspendResumeLock FAUX_LOCK = new SuspendResumeLock(false) {
@Override @Override
public void acquire() {} public void acquire() {}


Expand All @@ -47,7 +47,7 @@ public void resume() {}
/** /**
* Default constructor * Default constructor
*/ */
GlobalPoolLock(final boolean createSemaphore) { SuspendResumeLock(final boolean createSemaphore) {
acquisitionSemaphore = (createSemaphore ? new Semaphore(MAX_PERMITS, true) : null); acquisitionSemaphore = (createSemaphore ? new Semaphore(MAX_PERMITS, true) : null);
} }


Expand Down

0 comments on commit ef57e11

Please sign in to comment.