-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Julian Sareyka opened SPR-8849 and commented
Hi,
I use an embedded database (HSQL) in my integration test suite. Its a maven project with TestNG.
<jdbc:embedded-database id="mirDataSource">
<jdbc:script location="classpath:db/schema.sql" />
<jdbc:script location="classpath:db/tables_hsqldb.sql" />
<jdbc:script location="classpath:db/package_verweildauer.sql" />
<jdbc:script location="classpath:db/data.sql" />
</jdbc:embedded-database>I have one default context configuration (which includes the database) and several extensions:
for instance:
@ContextConfiguration("/spring/default.xml")
public class PodcastControllerTest extends AbstractTestNGSpringContextTests {...}
// and
@ContextConfiguration({ "/spring/components/amq-broker.xml", "/spring/default.xml" })
public class SophoraAvDocumentTest extends AbstractTestNGSpringContextTests {...}When executing the tests with maven (maven-surefire), all the tests with the first configuration reuse the same context (due to context caching). The first test-setup with the second configuration should create a fresh application context, but it fails with Database-Errors indicating, that the objects to create already exist. (I spent quite some time debugging the maven process).
Here is the maven surefire config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<threadCount>1</threadCount>
<excludedGroups>slow,local</excludedGroups>
<argLine>-Xmx1024m</argLine>
</configuration>
</plugin>My theory is, that both contexts connect to the same database instance, because they are in the same vm and the name of the db-instance is the same:
EmbeddedDatabaseFactory.databaseName = "testdb";
I did not find a way to assign the name with the jdbc:embedded-database-Tag.
So I created a custom EmbeddedDatabaseBuilder which assigns a random name (UUID) to every new instance:
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).setName(UUID.randomUUID().toString());
for (String sqlResource : this.scripts) {
builder.addScript(sqlResource);
}
this.embeddedDatabase = builder.build();With this one the errors are gone. Would it be a solution to add a configuration-option to jdbc:embedded-database for random db-instance names?
Affects: 3.1 GA
Issue Links:
- Introduce database-name attribute in <jdbc:embedded-database /> [SPR-12835] #17432 Introduce database-name attribute in <jdbc:embedded-database /> ("depends on")
- Document support for generating unique names for embedded databases in the reference manual [SPR-12839] #17437 Document support for generating unique names for embedded databases in the reference manual ("is depended on by")
- Embedded database connection closed by shutdown() method [SPR-11372] #15999 Embedded database connection closed by shutdown() method
- Using multiple declarations of jdbc:embedded-database creates only one H2 instance per JVM [SPR-8920] #13560 Using multiple declarations of jdbc:embedded-database creates only one H2 instance per JVM
- jdbc:datasource lacks 'name' attribute or related mechanism [SPR-7604] #12260 jdbc:datasource lacks 'name' attribute or related mechanism
6 votes, 7 watchers