Fix #288 allow explicit definition of driverClassName to override DriverManager.getDriver(url) located driver.

pull/294/head
Brett Wooldridge 10 years ago
parent 4faae6b8df
commit 76f80fd51f

@ -354,6 +354,11 @@ public abstract class AbstractHikariConfig implements HikariConfigMBean
dataSourceProperties.putAll(dsProperties);
}
public String getDriverClassName()
{
return driverClassName;
}
public void setDriverClassName(String driverClassName)
{
try {

@ -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());

@ -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;

@ -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<Driver> 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);

Loading…
Cancel
Save