Commit 26303a97 authored by Phillip Webb's avatar Phillip Webb

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.
parent c544921e
...@@ -183,7 +183,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware { ...@@ -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 { ...@@ -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");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment