From 6978694cb8e77156868caade62531cc1ec5c12ce Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sat, 3 Oct 2015 14:29:34 +0200 Subject: [PATCH 1/2] Improve error reporting when driver class version is unsupported ClassUtils.isPresent(String, ClassLoader) swallows all Throwables when trying to load a class by name. For this reason UnsupportedClassVersionError will also be swallowed when user code is trying to use a driver library which has been compiled with a later JDK than the one the application is running with. All the user would see was "Cannot load driver class". This change simply propagates the UnsupportedClassVersionNumberError so that it is easier for users to find the root cause of the problem. Closes gh-4082 Closes gh-4091 --- .../autoconfigure/jdbc/DataSourceProperties.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index 60ad720104..1030a051c3 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -161,7 +161,7 @@ public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAw public String getDriverClassName() { if (StringUtils.hasText(this.driverClassName)) { - Assert.state(ClassUtils.isPresent(this.driverClassName, null), + Assert.state(driverClassIsLoadable(), "Cannot load driver class: " + this.driverClassName); return this.driverClassName; } @@ -182,6 +182,20 @@ public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAw return driverClassName; } + private boolean driverClassIsLoadable() { + try { + ClassUtils.forName(this.driverClassName, null); + return true; + } + catch (UnsupportedClassVersionError ucve) { + // driver library has been compiled with a later JDK, propagate error + throw ucve; + } + catch (Throwable t) { + return false; + } + } + public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } From 2c2c7cee8d63b98d3c59d27d33a9f5944c411412 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 6 Oct 2015 09:54:13 +0100 Subject: [PATCH 2/2] Polish contribution See gh-4082 --- .../autoconfigure/jdbc/DataSourceProperties.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index 1030a051c3..7474567043 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -39,6 +39,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer * @author Maciej Walkowiak * @author Stephane Nicoll + * @author Benedikt Ritter * @since 1.1.0 */ @ConfigurationProperties(prefix = DataSourceProperties.PREFIX) @@ -161,8 +162,8 @@ public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAw public String getDriverClassName() { if (StringUtils.hasText(this.driverClassName)) { - Assert.state(driverClassIsLoadable(), - "Cannot load driver class: " + this.driverClassName); + Assert.state(driverClassIsLoadable(), "Cannot load driver class: " + + this.driverClassName); return this.driverClassName; } String driverClassName = null; @@ -187,11 +188,11 @@ public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAw ClassUtils.forName(this.driverClassName, null); return true; } - catch (UnsupportedClassVersionError ucve) { - // driver library has been compiled with a later JDK, propagate error - throw ucve; + catch (UnsupportedClassVersionError ex) { + // Driver library has been compiled with a later JDK, propagate error + throw ex; } - catch (Throwable t) { + catch (Throwable ex) { return false; } }