@@ -4623,16 +4623,17 @@ If you want to expose an embedded database instance as a bean in a Spring
4623
4623
[source,xml,indent=0]
4624
4624
[subs="verbatim,quotes"]
4625
4625
----
4626
- <jdbc:embedded-database id="dataSource">
4626
+ <jdbc:embedded-database id="dataSource" generate-name="true" >
4627
4627
<jdbc:script location="classpath:schema.sql"/>
4628
4628
<jdbc:script location="classpath:test-data.sql"/>
4629
4629
</jdbc:embedded-database>
4630
4630
----
4631
4631
4632
4632
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.
4636
4637
4637
4638
4638
4639
@@ -4641,24 +4642,28 @@ database instance is made available to the Spring container as a bean of type
4641
4642
4642
4643
The `EmbeddedDatabaseBuilder` class provides a fluent API for constructing an embedded
4643
4644
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.
4645
4646
4646
4647
[source,java,indent=0]
4647
4648
[subs="verbatim,quotes"]
4648
4649
----
4649
4650
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
4651
+ .generateUniqueName(true)
4650
4652
.setType(H2)
4651
4653
.setScriptEncoding("UTF-8")
4652
4654
.ignoreFailedDrops(true)
4653
4655
.addScript("schema.sql")
4654
4656
.addScripts("user_data.sql", "country_data.sql")
4655
4657
.build();
4656
4658
4657
- // do stuff against the db (EmbeddedDatabase extends javax.sql.DataSource)
4659
+ // perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource)
4658
4660
4659
4661
db.shutdown()
4660
4662
----
4661
4663
4664
+ Consult the Javadoc for `EmbeddedDatabaseBuilder` for further details on all supported
4665
+ options.
4666
+
4662
4667
The `EmbeddedDatabaseBuilder` can also be used to create an embedded database using Java
4663
4668
Config like in the following example.
4664
4669
@@ -4671,6 +4676,7 @@ public class DataSourceConfig {
4671
4676
@Bean
4672
4677
public DataSource dataSource() {
4673
4678
return new EmbeddedDatabaseBuilder()
4679
+ .generateUniqueName(true)
4674
4680
.setType(H2)
4675
4681
.setScriptEncoding("UTF-8")
4676
4682
.ignoreFailedDrops(true)
@@ -4731,7 +4737,10 @@ Framework>> and configuring the embedded database as a bean in the Spring
4731
4737
public void setUp() {
4732
4738
// creates an HSQL in-memory database populated from default scripts
4733
4739
// classpath:schema.sql and classpath:data.sql
4734
- db = new EmbeddedDatabaseBuilder().addDefaultScripts().build();
4740
+ db = new EmbeddedDatabaseBuilder()
4741
+ .generateUniqueName(true)
4742
+ .addDefaultScripts()
4743
+ .build();
4735
4744
}
4736
4745
4737
4746
@Test
@@ -4749,6 +4758,37 @@ Framework>> and configuring the embedded database as a bean in the Spring
4749
4758
----
4750
4759
4751
4760
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
+
4752
4792
4753
4793
[[jdbc-embedded-database-extension]]
4754
4794
==== Extending the embedded database support
0 commit comments