Skip to content

Commit

Permalink
Fixed #308 try harder at resolving the driver by various means when d…
Browse files Browse the repository at this point in the history
…riverClassName and jdbcUrl are both specified.
  • Loading branch information
brettwooldridge committed Apr 16, 2015
1 parent d781db4 commit 9c80913
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion hikaricp-common/pom.xml
Expand Up @@ -13,6 +13,6 @@
<parent> <parent>
<groupId>com.zaxxer</groupId> <groupId>com.zaxxer</groupId>
<artifactId>HikariCP-parent</artifactId> <artifactId>HikariCP-parent</artifactId>
<version>2.3.4-SNAPSHOT</version> <version>2.3.7-SNAPSHOT</version>
</parent> </parent>
</project> </project>
Expand Up @@ -27,53 +27,61 @@


import javax.sql.DataSource; import javax.sql.DataSource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class DriverDataSource implements DataSource public final class DriverDataSource implements DataSource
{ {
private final Logger LOGGER = LoggerFactory.getLogger(ConcurrentBag.class);

private final String jdbcUrl; private final String jdbcUrl;
private final Properties driverProperties; private final Properties driverProperties;
private final Driver driver; private Driver driver;


public DriverDataSource(String jdbcUrl, String driverClassName, Properties properties, String username, String password) public DriverDataSource(String jdbcUrl, String driverClassName, Properties properties, String username, String password)
{ {
try { this.jdbcUrl = jdbcUrl;
this.jdbcUrl = jdbcUrl; this.driverProperties = new Properties();
this.driverProperties = new Properties();


for (Entry<Object, Object> entry : properties.entrySet()) { for (Entry<Object, Object> entry : properties.entrySet()) {
driverProperties.setProperty(entry.getKey().toString(), entry.getValue().toString()); driverProperties.setProperty(entry.getKey().toString(), entry.getValue().toString());
} }


if (username != null) { if (username != null) {
driverProperties.put("user", driverProperties.getProperty("user", username)); driverProperties.put("user", driverProperties.getProperty("user", username));
} }
if (password != null) { if (password != null) {
driverProperties.put("password", driverProperties.getProperty("password", password)); driverProperties.put("password", driverProperties.getProperty("password", password));
} }


if (driverClassName != null) { if (driverClassName != null) {
Driver matched = null; Enumeration<Driver> drivers = DriverManager.getDrivers();
Enumeration<Driver> drivers = DriverManager.getDrivers(); while (drivers.hasMoreElements()) {
while (drivers.hasMoreElements()) { Driver d = drivers.nextElement();
Driver d = drivers.nextElement(); if (d.getClass().getName().equals(driverClassName)) {
if (d.getClass().getName().equals(driverClassName)) { this.driver = d;
matched = d;
break;
}
} }
}


if (matched != null) { if (driver == null) {
driver = matched; 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();
} }
else { catch (Exception e) {
throw new IllegalArgumentException("Driver with class name " + driverClassName + " was not found among registered drivers"); LOGGER.warn("Could not instantiate instance of driver class {}, trying JDBC URL resolution", driverClassName, e);
} }
} }
else { }

if (driver == null) {
try {
driver = DriverManager.getDriver(jdbcUrl); driver = DriverManager.getDriver(jdbcUrl);
} }
} catch (SQLException e) {
catch (SQLException e) { throw new RuntimeException("Unable to get driver instance for jdbcUrl=" + jdbcUrl, e);
throw new RuntimeException("Unable to get driver for JDBC URL " + jdbcUrl, e); }
} }
} }


Expand Down

0 comments on commit 9c80913

Please sign in to comment.