From 6978694cb8e77156868caade62531cc1ec5c12ce Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sat, 3 Oct 2015 14:29:34 +0200 Subject: [PATCH] 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; }