From 26303a97672318bd5d6cca4e103ac53f864c28d0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 25 Sep 2013 19:49:58 -0700 Subject: [PATCH] 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. --- .../jdbc/DataSourceAutoConfiguration.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java index 734970eff0..4d28bf8b15 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java @@ -183,7 +183,7 @@ public class DataSourceAutoConfiguration implements EnvironmentAware { } String driverClassName = getDriverClassName(context.getEnvironment(), - context.getClassLoader()); + getDataSourceClassLoader(context)); if (driverClassName == null) { return Outcome.noMatch("no database driver"); } @@ -200,6 +200,21 @@ public class DataSourceAutoConfiguration implements EnvironmentAware { 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) { String driverClassName = environment == null ? null : environment .getProperty("spring.database.driverClassName");