diff --git a/hikaricp-common/src/main/java/com/zaxxer/hikari/AbstractHikariConfig.java b/hikaricp-common/src/main/java/com/zaxxer/hikari/AbstractHikariConfig.java index d48b47fa..5471c835 100644 --- a/hikaricp-common/src/main/java/com/zaxxer/hikari/AbstractHikariConfig.java +++ b/hikaricp-common/src/main/java/com/zaxxer/hikari/AbstractHikariConfig.java @@ -354,6 +354,11 @@ public abstract class AbstractHikariConfig implements HikariConfigMBean dataSourceProperties.putAll(dsProperties); } + public String getDriverClassName() + { + return driverClassName; + } + public void setDriverClassName(String driverClassName) { try { diff --git a/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/BaseHikariPool.java b/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/BaseHikariPool.java index cf07dcb2..104bda13 100644 --- a/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/BaseHikariPool.java +++ b/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/BaseHikariPool.java @@ -150,7 +150,7 @@ public abstract class BaseHikariPool implements HikariPoolMBean, IBagStateListen setMetricRegistry(configuration.getMetricRegistry()); setHealthCheckRegistry(configuration.getHealthCheckRegistry()); - this.dataSource = poolUtils.initializeDataSource(configuration.getDataSourceClassName(), configuration.getDataSource(), configuration.getDataSourceProperties(), configuration.getJdbcUrl(), username, password); + this.dataSource = poolUtils.initializeDataSource(configuration.getDataSourceClassName(), configuration.getDataSource(), configuration.getDataSourceProperties(), configuration.getDriverClassName(), configuration.getJdbcUrl(), username, password); this.addConnectionExecutor = createThreadPoolExecutor(configuration.getMaximumPoolSize(), "HikariCP connection filler (pool " + configuration.getPoolName() + ")", configuration.getThreadFactory(), new ThreadPoolExecutor.DiscardPolicy()); this.closeConnectionExecutor = createThreadPoolExecutor(4, "HikariCP connection closer (pool " + configuration.getPoolName() + ")", configuration.getThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); diff --git a/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java b/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java index 2007b893..5194d211 100644 --- a/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java +++ b/hikaricp-common/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java @@ -90,12 +90,13 @@ public final class PoolUtilities * @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 */ - public DataSource initializeDataSource(final String dsClassName, DataSource dataSource, final Properties dataSourceProperties, final String jdbcUrl, final String username, final String password) + public DataSource initializeDataSource(final String dsClassName, DataSource dataSource, final Properties dataSourceProperties, final String driverClassName, final String jdbcUrl, final String username, final String password) { try { if (dataSource == null && dsClassName != null) { @@ -104,7 +105,7 @@ public final class PoolUtilities return dataSource; } else if (jdbcUrl != null) { - return new DriverDataSource(jdbcUrl, dataSourceProperties, username, password); + return new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password); } return dataSource; diff --git a/hikaricp-common/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java b/hikaricp-common/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java index fbbacd60..6e43948f 100644 --- a/hikaricp-common/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java +++ b/hikaricp-common/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java @@ -21,6 +21,7 @@ import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.util.Enumeration; import java.util.Map.Entry; import java.util.Properties; @@ -32,7 +33,7 @@ public final class DriverDataSource implements DataSource private final Properties driverProperties; private final Driver driver; - public DriverDataSource(String jdbcUrl, Properties properties, String username, String password) + public DriverDataSource(String jdbcUrl, String driverClassName, Properties properties, String username, String password) { try { this.jdbcUrl = jdbcUrl; @@ -49,7 +50,27 @@ public final class DriverDataSource implements DataSource driverProperties.put("password", driverProperties.getProperty("password", password)); } - driver = DriverManager.getDriver(jdbcUrl); + if (driverClassName != null) { + Driver matched = null; + Enumeration drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver d = drivers.nextElement(); + if (d.getClass().getName().equals(driverClassName)) { + matched = d; + break; + } + } + + if (matched != null) { + driver = matched; + } + else { + throw new IllegalArgumentException("Driver with class name " + driverClassName + " was not found among registered drivers"); + } + } + else { + driver = DriverManager.getDriver(jdbcUrl); + } } catch (SQLException e) { throw new RuntimeException("Unable to get driver for JDBC URL " + jdbcUrl, e);