<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=golliwog" rel="nofollow">Julian Sareyka</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8849?redirect=false" rel="nofollow">SPR-8849</a></strong> and commented</p>
<p dir="auto">Hi,<br>
I use an embedded database (HSQL) in my integration test suite. Its a maven project with TestNG.</p>
<div class="highlight highlight-text-xml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;jdbc:embedded-database id=&quot;mirDataSource&quot;&gt;
	&lt;jdbc:script location=&quot;classpath:db/schema.sql&quot; /&gt;
	&lt;jdbc:script location=&quot;classpath:db/tables_hsqldb.sql&quot; /&gt;
	&lt;jdbc:script location=&quot;classpath:db/package_verweildauer.sql&quot; /&gt;
	&lt;jdbc:script location=&quot;classpath:db/data.sql&quot; /&gt;
&lt;/jdbc:embedded-database&gt;"><pre class="notranslate">&lt;<span class="pl-ent">jdbc</span><span class="pl-ent">:</span><span class="pl-ent">embedded-database</span> <span class="pl-e">id</span>=<span class="pl-s"><span class="pl-pds">"</span>mirDataSource<span class="pl-pds">"</span></span>&gt;
	&lt;<span class="pl-ent">jdbc</span><span class="pl-ent">:</span><span class="pl-ent">script</span> <span class="pl-e">location</span>=<span class="pl-s"><span class="pl-pds">"</span>classpath:db/schema.sql<span class="pl-pds">"</span></span> /&gt;
	&lt;<span class="pl-ent">jdbc</span><span class="pl-ent">:</span><span class="pl-ent">script</span> <span class="pl-e">location</span>=<span class="pl-s"><span class="pl-pds">"</span>classpath:db/tables_hsqldb.sql<span class="pl-pds">"</span></span> /&gt;
	&lt;<span class="pl-ent">jdbc</span><span class="pl-ent">:</span><span class="pl-ent">script</span> <span class="pl-e">location</span>=<span class="pl-s"><span class="pl-pds">"</span>classpath:db/package_verweildauer.sql<span class="pl-pds">"</span></span> /&gt;
	&lt;<span class="pl-ent">jdbc</span><span class="pl-ent">:</span><span class="pl-ent">script</span> <span class="pl-e">location</span>=<span class="pl-s"><span class="pl-pds">"</span>classpath:db/data.sql<span class="pl-pds">"</span></span> /&gt;
&lt;/<span class="pl-ent">jdbc</span><span class="pl-ent">:</span><span class="pl-ent">embedded-database</span>&gt;</pre></div>
<p dir="auto">I have one default context configuration (which includes the database) and several extensions:<br>
for instance:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@ContextConfiguration(&quot;/spring/default.xml&quot;)
public class PodcastControllerTest extends AbstractTestNGSpringContextTests {...}

// and

@ContextConfiguration({ &quot;/spring/components/amq-broker.xml&quot;, &quot;/spring/default.xml&quot; })
public class SophoraAvDocumentTest extends AbstractTestNGSpringContextTests {...}"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">ContextConfiguration</span>(<span class="pl-s">"/spring/default.xml"</span>)
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-smi">PodcastControllerTest</span> <span class="pl-k">extends</span> <span class="pl-smi">AbstractTestNGSpringContextTests</span> {...}

<span class="pl-c">// and</span>

<span class="pl-c1">@</span><span class="pl-c1">ContextConfiguration</span>({ <span class="pl-s">"/spring/components/amq-broker.xml"</span>, <span class="pl-s">"/spring/default.xml"</span> })
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-smi">SophoraAvDocumentTest</span> <span class="pl-k">extends</span> <span class="pl-smi">AbstractTestNGSpringContextTests</span> {...}</pre></div>
<p dir="auto">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).</p>
<p dir="auto">Here is the maven surefire config:</p>
<div class="highlight highlight-text-xml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;plugin&gt;
	&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
	&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
	&lt;configuration&gt;
		&lt;threadCount&gt;1&lt;/threadCount&gt;
		&lt;excludedGroups&gt;slow,local&lt;/excludedGroups&gt;
		&lt;argLine&gt;-Xmx1024m&lt;/argLine&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;"><pre class="notranslate">&lt;<span class="pl-ent">plugin</span>&gt;
	&lt;<span class="pl-ent">groupId</span>&gt;org.apache.maven.plugins&lt;/<span class="pl-ent">groupId</span>&gt;
	&lt;<span class="pl-ent">artifactId</span>&gt;maven-surefire-plugin&lt;/<span class="pl-ent">artifactId</span>&gt;
	&lt;<span class="pl-ent">configuration</span>&gt;
		&lt;<span class="pl-ent">threadCount</span>&gt;1&lt;/<span class="pl-ent">threadCount</span>&gt;
		&lt;<span class="pl-ent">excludedGroups</span>&gt;slow,local&lt;/<span class="pl-ent">excludedGroups</span>&gt;
		&lt;<span class="pl-ent">argLine</span>&gt;-Xmx1024m&lt;/<span class="pl-ent">argLine</span>&gt;
	&lt;/<span class="pl-ent">configuration</span>&gt;
&lt;/<span class="pl-ent">plugin</span>&gt;</pre></div>
<p dir="auto">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:<br>
EmbeddedDatabaseFactory.databaseName = "testdb";</p>
<p dir="auto">I did not find a way to assign the name with the jdbc:embedded-database-Tag.</p>
<p dir="auto">So I created a custom EmbeddedDatabaseBuilder which assigns a random name (UUID) to every new instance:</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).setName(UUID.randomUUID().toString());
for (String sqlResource : this.scripts) {
    builder.addScript(sqlResource);
}
this.embeddedDatabase = builder.build();"><pre class="notranslate"><span class="pl-smi">EmbeddedDatabaseBuilder</span> <span class="pl-s1">builder</span> = <span class="pl-k">new</span> <span class="pl-smi">EmbeddedDatabaseBuilder</span>().<span class="pl-en">setType</span>(<span class="pl-smi">EmbeddedDatabaseType</span>.<span class="pl-c1">HSQL</span>).<span class="pl-en">setName</span>(<span class="pl-smi">UUID</span>.<span class="pl-en">randomUUID</span>().<span class="pl-en">toString</span>());
<span class="pl-k">for</span> (<span class="pl-smi">String</span> <span class="pl-s1">sqlResource</span> : <span class="pl-smi">this</span>.<span class="pl-s1">scripts</span>) {
    <span class="pl-s1">builder</span>.<span class="pl-en">addScript</span>(<span class="pl-s1">sqlResource</span>);
}
<span class="pl-smi">this</span>.<span class="pl-s1">embeddedDatabase</span> = <span class="pl-s1">builder</span>.<span class="pl-en">build</span>();</pre></div>
<p dir="auto">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?</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1 GA</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398177832" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/17432" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/17432/hovercard" href="https://github.com/spring-projects/spring-framework/issues/17432">#17432</a> Introduce database-name attribute in &lt;jdbc:embedded-database /&gt; (<em><strong>"depends on"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398177900" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/17437" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/17437/hovercard" href="https://github.com/spring-projects/spring-framework/issues/17437">#17437</a> Document support for generating unique names for embedded databases in the reference manual (<em><strong>"is depended on by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398165103" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/15999" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/15999/hovercard" href="https://github.com/spring-projects/spring-framework/issues/15999">#15999</a> Embedded database connection closed by shutdown() method</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398116015" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13560" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13560/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13560">#13560</a> Using multiple declarations of jdbc:embedded-database creates only one H2 instance per JVM</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398107795" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12260" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12260/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12260">#12260</a> jdbc:datasource lacks 'name' attribute or related mechanism</li>
</ul>
<p dir="auto">6 votes, 7 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=tschafer" rel="nofollow">Tim Schafer</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-671?redirect=false" rel="nofollow">SPR-671</a></strong> and commented</p>
<p dir="auto">2005-01-31 13:58:49 StandardContext[/kls2]Loading root WebApplicationContext<br>
2005-01-31 13:58:49 StandardContext[/kls2]Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener<br>
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/classpath:/database.beans.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/classpath:/database.beans.xml]<br>
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/classpath:/database.beans.xml]<br>
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:89)<br>
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:127)<br>
at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.importBeanDefinitionResource(DefaultXmlBeanDefinitionParser.java:226)<br>
at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.registerBeanDefinitions(DefaultXmlBeanDefinitionParser.java:180)<br>
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:170)<br>
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:129)<br>
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:144)<br>
at org.springframework.context.support.AbstractXmlApplicationContext.refreshBeanFactory(AbstractXmlApplicationContext.java:79)<br>
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:249)<br>
at org.springframework.web.context.support.XmlWebApplicationContext.refresh(XmlWebApplicationContext.java:131)<br>
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:167)<br>
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:101)<br>
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:48)<br>
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)<br>
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4343)<br>
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)<br>
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)<br>
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)<br>
at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277)<br>
at org.apache.catalina.core.StandardHost.install(StandardHost.java:832)<br>
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:922)<br>
at org.apache.catalina.manager.HTMLManagerServlet.deployInternal(HTMLManagerServlet.java:271)<br>
at org.apache.catalina.manager.HTMLManagerServlet.doGet(HTMLManagerServlet.java:95)<br>
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)<br>
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)<br>
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)<br>
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)<br>
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)<br>
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)<br>
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)<br>
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)<br>
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)<br>
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)<br>
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)<br>
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)<br>
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)<br>
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)<br>
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)<br>
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)<br>
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)<br>
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)<br>
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)<br>
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)<br>
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)<br>
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)<br>
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)<br>
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:300)<br>
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:374)<br>
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:743)<br>
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:675)<br>
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:866)<br>
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)<br>
at java.lang.Thread.run(Thread.java:534)</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 1.1.2</p>
<p dir="auto">1 votes, 0 watchers</p>