Skip to content

Introduce support for generating unique names for embedded databases [SPR-8849] #13491

@spring-projects-issues

Description

@spring-projects-issues

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:

6 votes, 7 watchers

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)in: testIssues in the test moduletype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions