Stop assuming datasource creation failure originates from auto-config

There are documented way to reuse bits of the infrastructure in user
config to offer similar datasource configuration. If that fails, the
regular failure there will kick in.

This commit improves `DataSourceBeanCreationFailureAnalyzer` to not
misguide users that the auto-configuration has failed. Rather, it
describes what has failed in a more generic way.

Closes gh-12947
This commit is contained in:
Stephane Nicoll
2018-05-03 11:46:27 +02:00
parent b3ad902902
commit 69ab956e8b
3 changed files with 16 additions and 9 deletions

View File

@@ -58,12 +58,12 @@ class DataSourceBeanCreationFailureAnalyzer
private String getDescription(DataSourceBeanCreationException cause) {
StringBuilder description = new StringBuilder();
description.append("Failed to auto-configure a DataSource: ");
if (!this.environment.containsProperty("spring.datasource.url")) {
description.append("'spring.datasource.url' is not specified and ");
description.append("Failed to configure a DataSource: ");
if (!StringUtils.hasText(cause.getProperties().getUrl())) {
description.append("'url' attribute is not specified and ");
}
description.append(
String.format("no embedded datasource could be auto-configured.%n"));
String.format("no embedded datasource could be configured.%n"));
description.append(String.format("%nReason: %s%n", cause.getMessage()));
return description.toString();
}

View File

@@ -235,7 +235,7 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
if (!StringUtils.hasText(driverClassName)) {
throw new DataSourceBeanCreationException(
"Failed to determine a suitable driver class",
this.embeddedDatabaseConnection);
this, this.embeddedDatabaseConnection);
}
return driverClassName;
}
@@ -282,7 +282,7 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
if (!StringUtils.hasText(url)) {
throw new DataSourceBeanCreationException(
"Failed to determine suitable jdbc url",
this.embeddedDatabaseConnection);
this, this.embeddedDatabaseConnection);
}
return url;
}
@@ -513,14 +513,21 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
static class DataSourceBeanCreationException extends BeanCreationException {
private final DataSourceProperties properties;
private final EmbeddedDatabaseConnection connection;
DataSourceBeanCreationException(String message,
DataSourceBeanCreationException(String message, DataSourceProperties properties,
EmbeddedDatabaseConnection connection) {
super(message);
this.properties = properties;
this.connection = connection;
}
public DataSourceProperties getProperties() {
return this.properties;
}
public EmbeddedDatabaseConnection getConnection() {
return this.connection;
}

View File

@@ -46,8 +46,8 @@ public class DataSourceBeanCreationFailureAnalyzerTests {
public void failureAnalysisIsPerformed() {
FailureAnalysis failureAnalysis = performAnalysis(TestConfiguration.class);
assertThat(failureAnalysis.getDescription()).contains(
"'spring.datasource.url' is not specified",
"no embedded datasource could be auto-configured",
"'url' attribute is not specified",
"no embedded datasource could be configured",
"Failed to determine a suitable driver class");
assertThat(failureAnalysis.getAction()).contains(
"If you want an embedded database (H2, HSQL or Derby), please put it on the classpath",