Browse Source
Development teams often encounter errors with embedded databases if their test suite inadvertently attempts to recreate additional instances of the same database. This can happen quite easily if an XML configuration file or @Configuration class is responsible for creating an embedded database and the corresponding configuration is then reused across multiple testing scenarios within the same test suite (i.e., within the same JVM process) -- for example, integration tests against embedded databases whose ApplicationContext configuration only differs with regard to which bean definition profiles are active. The root cause of such errors is the fact that Spring's EmbeddedDatabaseFactory (used internally by both the <jdbc:embedded-database> XML namespace element and the EmbeddedDatabaseBuilder for Java Config) will set the name of the embedded database to "testdb" if not otherwise specified. For the case of <jdbc:embedded-database>, the embedded database is typically assigned a name equal to the bean's id. Thus, subsequent attempts to create an embedded database will not result in a new database. Instead, the same JDBC connection URL will be reused, and attempts to create a new embedded database will actually point to an existing embedded database created from the same configuration. This commit addresses this common issue by introducing support for generating unique names for embedded databases. This support can be enabled via: - EmbeddedDatabaseFactory.setGenerateUniqueDatabaseName() - EmbeddedDatabaseBuilder.generateUniqueName() - <jdbc:embedded-database generate-name="true" ... > Issue: SPR-8849pull/759/head
Sam Brannen
10 years ago
9 changed files with 177 additions and 33 deletions
@ -0,0 +1,12 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns="http://www.springframework.org/schema/jdbc" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||||
|
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd"> |
||||||
|
|
||||||
|
<embedded-database id="dataSource" database-name="shouldBeOverriddenByGeneratedName" generate-name="true"> |
||||||
|
<script location="classpath:org/springframework/jdbc/config/db-schema.sql" /> |
||||||
|
<script location="classpath:org/springframework/jdbc/config/db-test-data.sql" /> |
||||||
|
</embedded-database> |
||||||
|
|
||||||
|
</beans:beans> |
Loading…
Reference in new issue