From 7ee3b287e1320c9092f152995bb139df0a674fc3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 17 Jun 2021 11:42:26 +0100 Subject: [PATCH] Catch isEmbedded failure and treat database as not embedded Fixes gh-26926 --- .../init/DataSourceScriptDatabaseInitializer.java | 13 ++++++++++++- .../DataSourceScriptDatabaseInitializerTests.java | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java index 6fe2993ac978..ccb131bc6872 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializer.java @@ -21,6 +21,9 @@ import javax.sql.DataSource; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer; @@ -38,6 +41,8 @@ */ public class DataSourceScriptDatabaseInitializer extends AbstractScriptDatabaseInitializer { + private static final Log logger = LogFactory.getLog(DataSourceScriptDatabaseInitializer.class); + private final DataSource dataSource; /** @@ -61,7 +66,13 @@ protected final DataSource getDataSource() { @Override protected boolean isEmbeddedDatabase() { - return EmbeddedDatabaseConnection.isEmbedded(this.dataSource); + try { + return EmbeddedDatabaseConnection.isEmbedded(this.dataSource); + } + catch (Exception ex) { + logger.debug("Could not determine if datasource is embedded", ex); + return false; + } } @Override diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java index 46a24b131654..26a86fe6e0ce 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/DataSourceScriptDatabaseInitializerTests.java @@ -22,6 +22,7 @@ import com.zaxxer.hikari.HikariDataSource; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializerTests; @@ -53,6 +54,13 @@ void closeDataSource() { this.standloneDataSource.close(); } + @Test + void whenDatabaseIsInaccessibleThenItIsAssumedNotToBeEmbedded() { + DataSourceScriptDatabaseInitializer initializer = new DataSourceScriptDatabaseInitializer( + new HikariDataSource(), new DatabaseInitializationSettings()); + assertThat(initializer.isEmbeddedDatabase()).isFalse(); + } + @Override protected DataSourceScriptDatabaseInitializer createEmbeddedDatabaseInitializer( DatabaseInitializationSettings settings) {