Skip to content

Commit bfdf6b7

Browse files
committed
Document unique names for embedded databases in reference manual
Issue: SPR-12839
1 parent fb07be5 commit bfdf6b7

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

Diff for: src/asciidoc/data-access.adoc

+47-7
Original file line numberDiff line numberDiff line change
@@ -4623,16 +4623,17 @@ If you want to expose an embedded database instance as a bean in a Spring
46234623
[source,xml,indent=0]
46244624
[subs="verbatim,quotes"]
46254625
----
4626-
<jdbc:embedded-database id="dataSource">
4626+
<jdbc:embedded-database id="dataSource" generate-name="true">
46274627
<jdbc:script location="classpath:schema.sql"/>
46284628
<jdbc:script location="classpath:test-data.sql"/>
46294629
</jdbc:embedded-database>
46304630
----
46314631

46324632
The preceding configuration creates an embedded HSQL database populated with SQL from
4633-
`schema.sql` and `test-data.sql` resources in the root of the root of the classpath. The
4634-
database instance is made available to the Spring container as a bean of type
4635-
`javax.sql.DataSource`. This bean can then be injected into data access objects as needed.
4633+
`schema.sql` and `test-data.sql` resources in the root of the classpath. In addition, as
4634+
a best practice, the embedded database will be assigned a uniquely generated name. The
4635+
embedded database is made available to the Spring container as a bean of type
4636+
`javax.sql.DataSource` which can then be injected into data access objects as needed.
46364637

46374638

46384639

@@ -4641,24 +4642,28 @@ database instance is made available to the Spring container as a bean of type
46414642

46424643
The `EmbeddedDatabaseBuilder` class provides a fluent API for constructing an embedded
46434644
database programmatically. Use this when you need to create an embedded database in a
4644-
standalone environment or in a standalone integration test:
4645+
standalone environment or in a standalone integration test like in the following example.
46454646

46464647
[source,java,indent=0]
46474648
[subs="verbatim,quotes"]
46484649
----
46494650
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
4651+
.generateUniqueName(true)
46504652
.setType(H2)
46514653
.setScriptEncoding("UTF-8")
46524654
.ignoreFailedDrops(true)
46534655
.addScript("schema.sql")
46544656
.addScripts("user_data.sql", "country_data.sql")
46554657
.build();
46564658
4657-
// do stuff against the db (EmbeddedDatabase extends javax.sql.DataSource)
4659+
// perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource)
46584660
46594661
db.shutdown()
46604662
----
46614663

4664+
Consult the Javadoc for `EmbeddedDatabaseBuilder` for further details on all supported
4665+
options.
4666+
46624667
The `EmbeddedDatabaseBuilder` can also be used to create an embedded database using Java
46634668
Config like in the following example.
46644669

@@ -4671,6 +4676,7 @@ public class DataSourceConfig {
46714676
@Bean
46724677
public DataSource dataSource() {
46734678
return new EmbeddedDatabaseBuilder()
4679+
.generateUniqueName(true)
46744680
.setType(H2)
46754681
.setScriptEncoding("UTF-8")
46764682
.ignoreFailedDrops(true)
@@ -4731,7 +4737,10 @@ Framework>> and configuring the embedded database as a bean in the Spring
47314737
public void setUp() {
47324738
// creates an HSQL in-memory database populated from default scripts
47334739
// classpath:schema.sql and classpath:data.sql
4734-
db = new EmbeddedDatabaseBuilder().addDefaultScripts().build();
4740+
db = new EmbeddedDatabaseBuilder()
4741+
.generateUniqueName(true)
4742+
.addDefaultScripts()
4743+
.build();
47354744
}
47364745
47374746
@Test
@@ -4749,6 +4758,37 @@ Framework>> and configuring the embedded database as a bean in the Spring
47494758
----
47504759

47514760

4761+
[[jdbc-embedded-database-unique-names]]
4762+
==== Generating unique names for embedded databases
4763+
4764+
Development teams often encounter errors with embedded databases if their test suite
4765+
inadvertently attempts to recreate additional instances of the same database. This can
4766+
happen quite easily if an XML configuration file or `@Configuration` class is responsible
4767+
for creating an embedded database and the corresponding configuration is then reused
4768+
across multiple testing scenarios within the same test suite (i.e., within the same JVM
4769+
process) –- for example, integration tests against embedded databases whose
4770+
`ApplicationContext` configuration only differs with regard to which bean definition
4771+
profiles are active.
4772+
4773+
The root cause of such errors is the fact that Spring's `EmbeddedDatabaseFactory` (used
4774+
internally by both the `<jdbc:embedded-database>` XML namespace element and the
4775+
`EmbeddedDatabaseBuilder` for Java Config) will set the name of the embedded database to
4776+
`"testdb"` if not otherwise specified. For the case of `<jdbc:embedded-database>`, the
4777+
embedded database is typically assigned a name equal to the bean's `id` (i.e., often
4778+
something like `"dataSource"`). Thus, subsequent attempts to create an embedded database
4779+
will not result in a new database. Instead, the same JDBC connection URL will be reused,
4780+
and attempts to create a new embedded database will actually point to an existing
4781+
embedded database created from the same configuration.
4782+
4783+
To address this common issue Spring Framework 4.2 provides support for generating
4784+
_unique_ names for embedded databases. To enable the use of generated names, use one of
4785+
the following options.
4786+
4787+
* `EmbeddedDatabaseFactory.setGenerateUniqueDatabaseName()`
4788+
* `EmbeddedDatabaseBuilder.generateUniqueName()`
4789+
* `<jdbc:embedded-database generate-name="true" ... >`
4790+
4791+
47524792

47534793
[[jdbc-embedded-database-extension]]
47544794
==== Extending the embedded database support

Diff for: src/asciidoc/whats-new.adoc

+3-5
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,6 @@ public @interface MyTestConfig {
607607
definition profiles.
608608
* Embedded databases can now be automatically assigned a unique name,
609609
allowing common test database configuration to be reused in different
610-
++ApplicationContext++s within a test suite. This support can be enabled
611-
via:
612-
** `EmbeddedDatabaseFactory.setGenerateUniqueDatabaseName()`
613-
** `EmbeddedDatabaseBuilder.generateUniqueName()`
614-
** `<jdbc:embedded-database generate-name="true" ... >`
610+
++ApplicationContext++s within a test suite.
611+
** See <<jdbc-embedded-database-unique-names>> for details.
612+

0 commit comments

Comments
 (0)