Skip to content

Commit cbb3418

Browse files
committed
Merge pull request #7004 from master
* pr/7004: Polish contribution Allow configuration to specify randomly generated database name
2 parents 0f97ccf + 03961e6 commit cbb3418

File tree

6 files changed

+108
-7
lines changed

6 files changed

+108
-7
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.charset.Charset;
2020
import java.util.LinkedHashMap;
2121
import java.util.Map;
22+
import java.util.UUID;
2223

2324
import javax.sql.DataSource;
2425

@@ -57,6 +58,11 @@ public class DataSourceProperties
5758
*/
5859
private String name = "testdb";
5960

61+
/**
62+
* Generate a random datasource name.
63+
*/
64+
private boolean generateUniqueName;
65+
6066
/**
6167
* Fully qualified name of the connection pool implementation to use. By default, it
6268
* is auto-detected from the classpath.
@@ -148,6 +154,8 @@ public class DataSourceProperties
148154

149155
private Xa xa = new Xa();
150156

157+
private String uniqueName;
158+
151159
@Override
152160
public void setBeanClassLoader(ClassLoader classLoader) {
153161
this.classLoader = classLoader;
@@ -183,6 +191,14 @@ public void setName(String name) {
183191
this.name = name;
184192
}
185193

194+
public boolean isGenerateUniqueName() {
195+
return this.generateUniqueName;
196+
}
197+
198+
public void setGenerateUniqueName(boolean generateUniqueName) {
199+
this.generateUniqueName = generateUniqueName;
200+
}
201+
186202
public Class<? extends DataSource> getType() {
187203
return this.type;
188204
}
@@ -268,14 +284,24 @@ public String determineUrl() {
268284
if (StringUtils.hasText(this.url)) {
269285
return this.url;
270286
}
271-
String url = this.embeddedDatabaseConnection.getUrl(this.name);
287+
String url = this.embeddedDatabaseConnection.getUrl(determineDatabaseName());
272288
if (!StringUtils.hasText(url)) {
273289
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
274290
this.environment, "url");
275291
}
276292
return url;
277293
}
278294

295+
private String determineDatabaseName() {
296+
if (this.generateUniqueName) {
297+
if (this.uniqueName == null) {
298+
this.uniqueName = UUID.randomUUID().toString();
299+
}
300+
return this.uniqueName;
301+
}
302+
return this.name;
303+
}
304+
279305
/**
280306
* Return the configured username or {@code null} if none was configured.
281307
* @return the configured username

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public void setBeanClassLoader(ClassLoader classLoader) {
5555
public EmbeddedDatabase dataSource() {
5656
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
5757
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
58-
this.database = builder.setName(this.properties.getName()).build();
58+
this.database = builder.setName(this.properties.getName())
59+
.generateUniqueName(this.properties.isGenerateUniqueName()).build();
5960
return this.database;
6061
}
6162

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ public void determineUrlWithExplicitConfig() throws Exception {
6666
assertThat(properties.determineUrl()).isEqualTo("jdbc:mysql://mydb");
6767
}
6868

69+
@Test
70+
public void determineUrlWithGenerateUniqueName() throws Exception {
71+
DataSourceProperties properties = new DataSourceProperties();
72+
properties.setGenerateUniqueName(true);
73+
properties.afterPropertiesSet();
74+
assertThat(properties.determineUrl()).isEqualTo(properties.determineUrl());
75+
76+
DataSourceProperties properties2 = new DataSourceProperties();
77+
properties2.setGenerateUniqueName(true);
78+
properties2.afterPropertiesSet();
79+
assertThat(properties.determineUrl()).isNotEqualTo(properties2.determineUrl());
80+
}
81+
6982
@Test
7083
public void determineUsername() throws Exception {
7184
DataSourceProperties properties = new DataSourceProperties();

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19+
import java.sql.Connection;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
1923
import javax.sql.DataSource;
2024

25+
import org.junit.After;
2126
import org.junit.Test;
2227

28+
import org.springframework.boot.test.util.EnvironmentTestUtils;
2329
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2430

2531
import static org.assertj.core.api.Assertions.assertThat;
@@ -28,18 +34,64 @@
2834
* Tests for {@link EmbeddedDataSourceConfiguration}.
2935
*
3036
* @author Dave Syer
37+
* @author Stephane Nicoll
3138
*/
3239
public class EmbeddedDataSourceConfigurationTests {
3340

3441
private AnnotationConfigApplicationContext context;
3542

43+
@After
44+
public void closeContext() {
45+
if (this.context != null) {
46+
this.context.close();
47+
}
48+
}
49+
3650
@Test
37-
public void testDefaultEmbeddedDatabase() throws Exception {
38-
this.context = new AnnotationConfigApplicationContext();
39-
this.context.register(EmbeddedDataSourceConfiguration.class);
40-
this.context.refresh();
51+
public void defaultEmbeddedDatabase() {
52+
this.context = load();
4153
assertThat(this.context.getBean(DataSource.class)).isNotNull();
42-
this.context.close();
54+
}
55+
56+
@Test
57+
public void generateUniqueName() throws Exception {
58+
this.context = load("spring.datasource.generate-unique-name=true");
59+
AnnotationConfigApplicationContext context2 =
60+
load("spring.datasource.generate-unique-name=true");
61+
try {
62+
DataSource dataSource = this.context.getBean(DataSource.class);
63+
DataSource dataSource2 = context2.getBean(DataSource.class);
64+
assertThat(getDatabaseName(dataSource))
65+
.isNotEqualTo(getDatabaseName(dataSource2));
66+
System.out.println(dataSource2);
67+
}
68+
finally {
69+
context2.close();
70+
}
71+
}
72+
73+
private String getDatabaseName(DataSource dataSource) throws SQLException {
74+
Connection connection = dataSource.getConnection();
75+
try {
76+
ResultSet catalogs = connection.getMetaData().getCatalogs();
77+
if (catalogs.next()) {
78+
return catalogs.getString(1);
79+
}
80+
else {
81+
throw new IllegalStateException("Unable to get database name");
82+
}
83+
}
84+
finally {
85+
connection.close();
86+
}
87+
}
88+
89+
private AnnotationConfigApplicationContext load(String... environment) {
90+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
91+
EnvironmentTestUtils.addEnvironment(ctx, environment);
92+
ctx.register(EmbeddedDataSourceConfiguration.class);
93+
ctx.refresh();
94+
return ctx;
4395
}
4496

4597
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ content into your application; rather pick only the properties that you need.
620620
spring.datasource.dbcp.*= # Commons DBCP specific settings
621621
spring.datasource.dbcp2.*= # Commons DBCP2 specific settings
622622
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
623+
spring.datasource.generate-unique-name=false # Generate a random datasource name.
623624
spring.datasource.hikari.*= # Hikari specific settings
624625
spring.datasource.initialize=true # Populate the database using 'data.sql'.
625626
spring.datasource.jmx-enabled=false # Enable JMX support (if provided by the underlying pool).

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,14 @@ http://hsqldb.org/[HSQL] and http://db.apache.org/derby/[Derby] databases. You d
26552655
to provide any connection URLs, simply include a build dependency to the embedded database
26562656
that you want to use.
26572657

2658+
[NOTE]
2659+
====
2660+
If you are using this feature in your tests, you may notice that the same database is
2661+
reused by your whole test suite regardless of the number of application contexts that
2662+
you use. If you want to make sure that each context has a separate embedded database,
2663+
you should set `spring.datasource.generate-unique-name` to `true`.
2664+
====
2665+
26582666
For example, typical POM dependencies would be:
26592667

26602668
[source,xml,indent=0]

0 commit comments

Comments
 (0)