diff --git a/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java b/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java index 184c5beb..68cb993e 100644 --- a/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java +++ b/src/main/java/com/zaxxer/hikari/pool/PoolUtilities.java @@ -105,23 +105,19 @@ public final class PoolUtilities */ 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) { - dataSource = createInstance(dsClassName, DataSource.class); - PropertyBeanSetter.setTargetFromProperties(dataSource, dataSourceProperties); - return dataSource; - } - else if (jdbcUrl != null) { - return new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password); - } - - return dataSource; + if (dsClassName != null && dataSource == null) { + dataSource = createInstance(dsClassName, DataSource.class); + PropertyBeanSetter.setTargetFromProperties(dataSource, dataSourceProperties); } - finally { - if (dataSource != null) { - createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl); - } + else if (jdbcUrl != null && dataSource == null) { + dataSource = new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password); } + + if (dataSource != null) { + createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl); + } + + return dataSource; } /** diff --git a/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java b/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java index 031b6a0c..39af90cf 100644 --- a/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java +++ b/src/main/java/com/zaxxer/hikari/util/DriverDataSource.java @@ -27,11 +27,16 @@ import java.util.Properties; import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public final class DriverDataSource implements DataSource { + private final Logger LOGGER = LoggerFactory.getLogger(DriverDataSource.class); + private final String jdbcUrl; private final Properties driverProperties; - private final Driver driver; + private Driver driver; public DriverDataSource(String jdbcUrl, String driverClassName, Properties properties, String username, String password) { @@ -50,29 +55,33 @@ public final class DriverDataSource implements DataSource } 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; + this.driver = d; break; } } - if (matched != null) { - driver = matched; - } - else { - throw new IllegalArgumentException("Driver with class name " + driverClassName + " was not found among registered drivers"); + if (driver == null) { + LOGGER.warn("Registered driver with driverClassName={} was not found, trying direct instantiation.", driverClassName); + try { + Class driverClass = this.getClass().getClassLoader().loadClass(driverClassName); + this.driver = (Driver) driverClass.newInstance(); + } + catch (Exception e) { + LOGGER.warn("Could not instantiate instance of driver class {}, trying JDBC URL resolution", driverClassName, e); + } } } - else { + + if (driver == null) { try { driver = DriverManager.getDriver(jdbcUrl); } catch (SQLException e) { - throw new RuntimeException("Unable to get driver for JDBC URL " + jdbcUrl, e); + throw new RuntimeException("Unable to get driver instance for jdbcUrl=" + jdbcUrl, e); } } } diff --git a/src/test/java/com/zaxxer/hikari/TestJNDI.java b/src/test/java/com/zaxxer/hikari/TestJNDI.java index 6985ceac..f6003353 100644 --- a/src/test/java/com/zaxxer/hikari/TestJNDI.java +++ b/src/test/java/com/zaxxer/hikari/TestJNDI.java @@ -25,6 +25,8 @@ import org.junit.Assert; import org.junit.Test; import org.osjava.sj.jndi.AbstractContext; +import com.zaxxer.hikari.mocks.StubDataSource; + public class TestJNDI { @Test @@ -61,7 +63,7 @@ public class TestJNDI ref.add(new BogusRef("maxLifetime", "20000")); ref.add(new BogusRef("maximumPoolSize", "10")); ref.add(new BogusRef("dataSource.loginTimeout", "10")); - Context nameCtx = new BogusContext(); + Context nameCtx = new BogusContext2(); HikariDataSource ds = (HikariDataSource) jndi.getObjectInstance(ref, null, nameCtx, null); Assert.assertNotNull(ds); @@ -99,6 +101,21 @@ public class TestJNDI } } + private class BogusContext2 extends AbstractContext + { + @Override + public Context createSubcontext(Name name) throws NamingException + { + return null; + } + + @Override + public Object lookup(String name) throws NamingException + { + return new StubDataSource(); + } + } + private class BogusRef extends RefAddr { private static final long serialVersionUID = 1L;