Skip to content

Commit 9ff50f9

Browse files
Default spring.datasource.generate-unique-name to true
This change ensures that each test in a test suite that shares an application context gets a unique embedded database, to prevent inconsistent embedded database state between tests. Closes gh-16747
1 parent 951d0b0 commit 9ff50f9

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
* @author Stephane Nicoll
4545
* @author Benedikt Ritter
4646
* @author Eddú Meléndez
47+
* @author Scott Frederick
4748
* @since 1.1.0
4849
*/
4950
@ConfigurationProperties(prefix = "spring.datasource")
@@ -59,7 +60,7 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
5960
/**
6061
* Whether to generate a random datasource name.
6162
*/
62-
private boolean generateUniqueName;
63+
private boolean generateUniqueName = true;
6364

6465
/**
6566
* Fully qualified name of the connection pool implementation to use. By default, it

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
* @author Maciej Walkowiak
3131
* @author Stephane Nicoll
3232
* @author Eddú Meléndez
33+
* @author Scott Frederick
3334
*/
3435
class DataSourcePropertiesTests {
3536

@@ -51,8 +52,9 @@ void determineDriverWithExplicitConfig() {
5152
}
5253

5354
@Test
54-
void determineUrl() throws Exception {
55+
void determineUrlWithoutGenerateUniqueName() throws Exception {
5556
DataSourceProperties properties = new DataSourceProperties();
57+
properties.setGenerateUniqueName(false);
5658
properties.afterPropertiesSet();
5759
assertThat(properties.getUrl()).isNull();
5860
assertThat(properties.determineUrl()).isEqualTo(EmbeddedDatabaseConnection.H2.getUrl("testdb"));
@@ -79,7 +81,6 @@ void determineUrlWithExplicitConfig() throws Exception {
7981
@Test
8082
void determineUrlWithGenerateUniqueName() throws Exception {
8183
DataSourceProperties properties = new DataSourceProperties();
82-
properties.setGenerateUniqueName(true);
8384
properties.afterPropertiesSet();
8485
assertThat(properties.determineUrl()).isEqualTo(properties.determineUrl());
8586

0 commit comments

Comments
 (0)