Ensure DataSource can load database driver

Update DataSource conditional to ensure that the driver class
can actually be loaded by the DataSource. This fixes an issue when
deploying a classic WAR where `org.apache.tomcat.jdbc.pool.DataSource`
is found the parent classloader but the database driver cannot be loaded
because is included as a local `/lib` dependency.
pull/57/merge
Phillip Webb 11 years ago
parent c544921eaa
commit 26303a9767

@ -183,7 +183,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
} }
String driverClassName = getDriverClassName(context.getEnvironment(), String driverClassName = getDriverClassName(context.getEnvironment(),
context.getClassLoader()); getDataSourceClassLoader(context));
if (driverClassName == null) { if (driverClassName == null) {
return Outcome.noMatch("no database driver"); return Outcome.noMatch("no database driver");
} }
@ -200,6 +200,21 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
return Outcome.noMatch("missing database driver " + driverClassName); return Outcome.noMatch("missing database driver " + driverClassName);
} }
/**
* Returns the class loader for the {@link DataSource} class. Used to ensure that
* the driver class can actually be loaded by the data source.
*/
private ClassLoader getDataSourceClassLoader(ConditionContext context) {
try {
Class<?> dataSourceClass = ClassUtils.forName(getDataSourceClassName(),
context.getClassLoader());
return dataSourceClass.getClassLoader();
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(ex);
}
}
private String getDriverClassName(Environment environment, ClassLoader classLoader) { private String getDriverClassName(Environment environment, ClassLoader classLoader) {
String driverClassName = environment == null ? null : environment String driverClassName = environment == null ? null : environment
.getProperty("spring.database.driverClassName"); .getProperty("spring.database.driverClassName");

Loading…
Cancel
Save