Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
4.1.2
Browse files Browse the repository at this point in the history
- Bug fixes and improvements
  • Loading branch information
Foulest committed Aug 5, 2024
1 parent ec6e7e7 commit 5f58966
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 89 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.zaxxer'
version = '4.1.1'
version = '4.1.2'
description = project.name

compileJava.options.encoding = 'UTF-8'
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/zaxxer/hikari/HikariConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.zaxxer.hikari.util.Credentials;
import com.zaxxer.hikari.util.PropertyElf;
import com.zaxxer.hikari.util.UtilityElf;
import lombok.Cleanup;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -44,6 +43,7 @@
import java.nio.file.Paths;
import java.security.AccessControlException;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;
import java.util.TreeSet;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -105,13 +105,13 @@ public class HikariConfig implements HikariConfigMXBean {
private boolean registerMbeans;
private boolean allowPoolSuspension;
private DataSource dataSource;
private final Properties dataSourceProperties;
private final Map<Object, Object> dataSourceProperties;
private ThreadFactory threadFactory;
private ScheduledExecutorService scheduledExecutor;
private MetricsTrackerFactory metricsTrackerFactory;
private Object metricRegistry;
private Object healthCheckRegistry;
private final Properties healthCheckProperties;
private final Map<Object, Object> healthCheckProperties;
private long keepaliveTime;

private volatile boolean sealed;
Expand Down Expand Up @@ -145,7 +145,7 @@ public HikariConfig() {
*
* @param properties the name of the property file
*/
public HikariConfig(Properties properties) {
public HikariConfig(Map<Object, Object> properties) {
this();
PropertyElf.setTargetFromProperties(this, properties);
}
Expand Down Expand Up @@ -318,9 +318,9 @@ public void setDataSourceJNDI(String jndiDataSource) {
dataSourceJNDI = jndiDataSource;
}

public void setDataSourceProperties(Properties dsProperties) {
public void setDataSourceProperties(Map<Object, Object> properties) {
checkIfSealed();
dataSourceProperties.putAll(dsProperties);
dataSourceProperties.putAll(properties);
}

public void setDriverClassName(String driverClassName) {
Expand Down Expand Up @@ -479,14 +479,14 @@ public void setHealthCheckRegistry(Object healthCheckRegistry) {
this.healthCheckRegistry = healthCheckRegistry;
}

public void setHealthCheckProperties(Properties healthCheckProperties) {
public void setHealthCheckProperties(Map<Object, Object> properties) {
checkIfSealed();
this.healthCheckProperties.putAll(healthCheckProperties);
healthCheckProperties.putAll(properties);
}

public void addHealthCheckProperty(String key, String value) {
checkIfSealed();
healthCheckProperties.setProperty(key, value);
healthCheckProperties.put(key, value);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/zaxxer/hikari/HikariDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.sql.SQLFeatureNotSupportedException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

/**
* The HikariCP pooled DataSource.
Expand Down Expand Up @@ -135,7 +136,7 @@ public Connection getConnection() throws SQLException {

@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new SQLFeatureNotSupportedException();
throw new SQLFeatureNotSupportedException("HikariDataSource.getConnection is not supported");
}

@Override
Expand Down Expand Up @@ -169,8 +170,8 @@ public int getLoginTimeout() throws SQLException {
}

@Override
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("HikariDataSource.getParentLogger is not supported");
}

@Override
Expand Down Expand Up @@ -312,10 +313,13 @@ public HikariConfigMXBean getHikariConfigMXBean() {
* @param connection the connection to evict from the pool
*/
public void evictConnection(Connection connection) {
HikariPool hikariPool;
if (isClosed()) {
return;
}

HikariPool hikariPool = pool.get();

if (!isClosed() && (hikariPool = pool.get()) != null
&& connection.getClass().getName().startsWith("com.zaxxer.hikari")) {
if (hikariPool != null && connection.getClass().getName().startsWith("com.zaxxer.hikari")) {
hikariPool.evictConnection(connection);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.jetbrains.annotations.NotNull;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
Expand All @@ -40,7 +43,7 @@
* @author Brett Wooldridge, Luca Burgazzoli
*/
@Slf4j
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "WeakerAccess"})
public class HikariConnectionProvider implements ConnectionProvider, Configurable, Stoppable {

private static final long serialVersionUID = -9131625057941275711L;
Expand Down Expand Up @@ -127,4 +130,12 @@ public <T> T unwrap(@NotNull Class<T> unwrapType) {
public void stop() {
dataSource.close();
}

private void writeObject(@NotNull ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
}

private void readObject(@NotNull ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -60,16 +60,16 @@ public final class CodahaleHealthChecker {
public static void registerHealthChecks(HikariPool pool,
@NotNull HikariConfig hikariConfig,
@NotNull HealthCheckRegistry registry) {
Properties healthCheckProperties = hikariConfig.getHealthCheckProperties();
Map<Object, Object> healthCheckProperties = hikariConfig.getHealthCheckProperties();
MetricRegistry metricRegistry = (MetricRegistry) hikariConfig.getMetricRegistry();

long checkTimeoutMs = Long.parseLong(healthCheckProperties.getProperty("connectivityCheckTimeoutMs",
long checkTimeoutMs = Long.parseLong((String) healthCheckProperties.getOrDefault("connectivityCheckTimeoutMs",
String.valueOf(hikariConfig.getConnectionTimeout())));

registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "ConnectivityCheck"),
new ConnectivityHealthCheck(pool, checkTimeoutMs));

long expected99thPercentile = Long.parseLong(healthCheckProperties.getProperty(
long expected99thPercentile = Long.parseLong((String) healthCheckProperties.getOrDefault(
"expected99thPercentileMs", "0"));

if (metricRegistry != null && expected99thPercentile > 0) {
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com/zaxxer/hikari/pool/HikariPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTransientConnectionException;
Expand Down Expand Up @@ -73,8 +76,8 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, Conc
private static final String EVICTED_CONNECTION_MESSAGE = "(connection was evicted)";
private static final String DEAD_CONNECTION_MESSAGE = "(connection is dead)";

private final PoolEntryCreator poolEntryCreator = new PoolEntryCreator(null /*logging prefix*/);
private final PoolEntryCreator postFillPoolEntryCreator = new PoolEntryCreator("After adding ");
private final Callable<Boolean> poolEntryCreator = new PoolEntryCreator(null /*logging prefix*/);
private final Callable<Boolean> postFillPoolEntryCreator = new PoolEntryCreator("After adding ");
private final Collection<Runnable> addConnectionQueueReadOnlyView;
private final ThreadPoolExecutor addConnectionExecutor;
private final ThreadPoolExecutor closeConnectionExecutor;
Expand Down Expand Up @@ -511,14 +514,14 @@ private void fillPool() {
/**
* Attempt to abort or close active connections.
*
* @param assassinExecutor the ExecutorService to pass to Connection.abort()
* @param executor the ExecutorService to pass to Connection.abort()
*/
private void abortActiveConnections(ExecutorService assassinExecutor) {
private void abortActiveConnections(Executor executor) {
for (PoolEntry poolEntry : connectionBag.values(ConcurrentBag.IConcurrentBagEntry.STATE_IN_USE)) {
Connection connection = poolEntry.close();

try {
connection.abort(assassinExecutor);
connection.abort(executor);
} catch (SQLException ex) {
quietlyCloseConnection(connection, "(connection aborted during shutdown)");
} finally {
Expand Down Expand Up @@ -859,5 +862,13 @@ public static class PoolInitializationException extends RuntimeException {
public PoolInitializationException(Throwable ex) {
super("Failed to initialize pool: " + ex.getMessage(), ex);
}

private void writeObject(@NotNull ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
}

private void readObject(@NotNull ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}
}
94 changes: 66 additions & 28 deletions src/main/java/com/zaxxer/hikari/pool/PoolBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
import javax.management.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.CommonDataSource;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTransientConnectionException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -310,7 +314,7 @@ private void initializeDataSource() {
String dsClassName = config.getDataSourceClassName();
String driverClassName = config.getDriverClassName();
String dataSourceJNDI = config.getDataSourceJNDI();
Properties dataSourceProperties = config.getDataSourceProperties();
Map<Object, Object> dataSourceProperties = config.getDataSourceProperties();
DataSource ds = config.getDataSource();

if (dsClassName != null && ds == null) {
Expand Down Expand Up @@ -357,37 +361,63 @@ private void initializeDataSource() {
Connection connection = null;

try {
Credentials credentials = config.getCredentials();
String username = credentials.getUsername();
String password = credentials.getPassword();

connection = (username == null)
? unwrappedDataSource.getConnection()
: unwrappedDataSource.getConnection(username, password);

if (connection == null) {
throw new SQLTransientConnectionException("DataSource returned null unexpectedly");
}

connection = createConnection();
setupConnection(connection);
lastConnectionFailure.set(null);
return connection;

} catch (SQLTransientConnectionException | ConnectionSetupException ex) {
if (connection != null) {
quietlyCloseConnection(connection, "(Failed to create/setup connection)");
} else if (getLastConnectionFailure() == null) {
log.debug("{} - Failed to create/setup connection: {}", poolName, ex.getMessage());
}

lastConnectionFailure.set(ex);
handleConnectionException(connection, ex);
throw ex;

} finally {
// tracker will be null during failFast check
if (metricsTracker != null) {
metricsTracker.recordConnectionCreated(ClockSource.elapsedMillis(start));
}
recordMetrics(start);
}
}

/**
* Create a new connection using the data source and credentials.
*
* @return a new Connection
* @throws SQLException if a database access error occurs
*/
private @NotNull Connection createConnection() throws SQLException {
Credentials credentials = config.getCredentials();
String username = credentials.getUsername();
String password = credentials.getPassword();

Connection connection = (username == null)
? unwrappedDataSource.getConnection()
: unwrappedDataSource.getConnection(username, password);

if (connection == null) {
throw new SQLTransientConnectionException("DataSource returned null unexpectedly");
}
return connection;
}

/**
* Handle exceptions that occur during connection setup.
*
* @param connection the connection that was attempted to be created
* @param ex the exception that was thrown
*/
private void handleConnectionException(Connection connection, Exception ex) {
if (connection != null) {
quietlyCloseConnection(connection, "(Failed to create/setup connection)");
} else if (getLastConnectionFailure() == null) {
log.debug("{} - Failed to create/setup connection: {}", poolName, ex.getMessage());
}

lastConnectionFailure.set(ex);
}

/**
* Record metrics for connection creation time.
*
* @param start the start time of the connection creation
*/
private void recordMetrics(long start) {
if (metricsTracker != null) {
metricsTracker.recordConnectionCreated(ClockSource.elapsedMillis(start));
}
}

Expand Down Expand Up @@ -608,7 +638,7 @@ private void createNetworkTimeoutExecutor(DataSource dataSource, String dsClassN
*
* @param dataSource the DataSource
*/
private void setLoginTimeout(DataSource dataSource) {
private void setLoginTimeout(CommonDataSource dataSource) {
if (connectionTimeout != Integer.MAX_VALUE) {
try {
dataSource.setLoginTimeout(Math.max(1, (int) TimeUnit.MILLISECONDS.toSeconds(500L + connectionTimeout)));
Expand Down Expand Up @@ -653,6 +683,14 @@ static class ConnectionSetupException extends Exception {
ConnectionSetupException(Throwable ex) {
super(ex);
}

private void writeObject(@NotNull ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
}

private void readObject(@NotNull ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.zaxxer.hikari.pool;

import java.sql.CallableStatement;
import java.sql.Statement;

/**
* This is the proxy class for java.sql.CallableStatement.
Expand All @@ -27,7 +28,7 @@
*/
public abstract class ProxyCallableStatement extends ProxyPreparedStatement implements CallableStatement {

protected ProxyCallableStatement(ProxyConnection connection, CallableStatement statement) {
protected ProxyCallableStatement(ProxyConnection connection, Statement statement) {
super(connection, statement);
}
}
Loading

0 comments on commit 5f58966

Please sign in to comment.