Merge branch '2.3.x' into dev

* 2.3.x:
  Fix invalid JNDI test
  Allow a specifically set DataSource to override other settings.  Also, in the case of a driver-based config, we were not initialising the network timeout executor property.
  Fixed #308 try harder at resolving the driver by various means when driverClassName and jdbcUrl are both specified.
  Fixed #308 try harder at resolving the driver by various means when driverClassName and jdbcUrl are both specified.
pull/316/merge
Brett Wooldridge 10 years ago
commit 56016a99cc

@ -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;
}
/**

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

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

Loading…
Cancel
Save