Skip to content

Commit 46ae53f

Browse files
committed
Fix for Can't connect to a local h2 database created without username #23538", the url is now added to the isEmbedded() and for the autoconfiguration of the database - the primary db rewire is set to true.
1 parent 0c1ea51 commit 46ae53f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public String determineUsername() {
325325
if (StringUtils.hasText(this.username)) {
326326
return this.username;
327327
}
328-
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
328+
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())) {
329329
return "sa";
330330
}
331331
return null;
@@ -353,7 +353,7 @@ public String determinePassword() {
353353
if (StringUtils.hasText(this.password)) {
354354
return this.password;
355355
}
356-
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
356+
if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName(), determineUrl())) {
357357
return "";
358358
}
359359
return null;

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private void process(BeanDefinitionRegistry registry, ConfigurableListableBeanFa
101101

102102
private BeanDefinition createEmbeddedBeanDefinition(boolean primary) {
103103
BeanDefinition beanDefinition = new RootBeanDefinition(EmbeddedDataSourceFactoryBean.class);
104-
beanDefinition.setPrimary(primary);
104+
beanDefinition.setPrimary(true);
105105
return beanDefinition;
106106
}
107107

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2929
import org.springframework.util.Assert;
3030
import org.springframework.util.ClassUtils;
31+
import org.springframework.util.StringUtils;
3132

3233
/**
3334
* Connection details for {@link EmbeddedDatabaseType embedded databases}.
@@ -122,10 +123,21 @@ public String getUrl(String databaseName) {
122123
* @param driverClass the driver class
123124
* @return true if the driver class is one of the embedded types
124125
*/
125-
public static boolean isEmbedded(String driverClass) {
126+
@Deprecated public static boolean isEmbedded(String driverClass) {
126127
return driverClass != null && (matches(HSQL, driverClass) || matches(H2, driverClass)
127128
|| matches(DERBY, driverClass) || matches(HSQLDB, driverClass));
128129
}
130+
/**
131+
* Convenience method to determine if a given driver class name and url represents an embedded
132+
* database type.The exception is made for the H2 database for embedded types.
133+
* @param driverClass the driver class
134+
* @param url the jdbc url
135+
* @return true if the driver class is one of the embedded types
136+
*/
137+
public static boolean isEmbedded(String driverClass, String url) {
138+
return (driverClass != null && (matches(HSQL, driverClass) || ( matches(H2, driverClass) && !StringUtils.hasText("mem"))
139+
|| matches(DERBY, driverClass) || matches(HSQLDB, driverClass)));
140+
}
129141

130142
private static boolean matches(EmbeddedDatabaseConnection candidate, String driverClass) {
131143
return driverClass.equals(candidate.driverClass) || driverClass.equals(candidate.alternativeDriverClass);

0 commit comments

Comments
 (0)