Skip to content

Commit 8aefcb9

Browse files
committed
Set DB_CLOSE_ON_EXIT=false in H2EmbeddedDbConfig
This commit sets the DB_CLOSE_ON_EXIT flag to false for embedded H2 databases loaded using H2EmbeddedDatabaseConfigurer (i.e., via Spring's <jdbc:embedded-database /> XML namespace, EmbeddedDatabaseBuilder, EmbeddedDatabaseFactory, and EmbeddedDatabaseFactoryBean). Issue: SPR-11573
1 parent dc6d675 commit 8aefcb9

File tree

3 files changed

+69
-47
lines changed

3 files changed

+69
-47
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Oliver Gierke
2828
* @author Juergen Hoeller
29+
* @author Sam Brannen
2930
* @since 3.0
3031
*/
3132
final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigurer {
@@ -36,7 +37,7 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu
3637

3738

3839
/**
39-
* Get the singleton {@link H2EmbeddedDatabaseConfigurer} instance.
40+
* Get the singleton {@code H2EmbeddedDatabaseConfigurer} instance.
4041
* @return the configurer
4142
* @throws ClassNotFoundException if H2 is not on the classpath
4243
*/
@@ -57,7 +58,7 @@ private H2EmbeddedDatabaseConfigurer(Class<? extends Driver> driverClass) {
5758
@Override
5859
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
5960
properties.setDriverClass(this.driverClass);
60-
properties.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", databaseName));
61+
properties.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false", databaseName));
6162
properties.setUsername("sa");
6263
properties.setPassword("");
6364
}
Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,123 +42,130 @@
4242
* @author Dave Syer
4343
* @author Juergen Hoeller
4444
* @author Chris Beams
45+
* @author Sam Brannen
4546
*/
4647
public class JdbcNamespaceIntegrationTests {
4748

4849
@Rule
4950
public ExpectedException expected = ExpectedException.none();
5051

52+
5153
@Test
52-
public void testCreateEmbeddedDatabase() throws Exception {
54+
public void createEmbeddedDatabase() throws Exception {
5355
Assume.group(TestGroup.LONG_RUNNING);
54-
55-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
56-
"org/springframework/jdbc/config/jdbc-config.xml");
57-
assertCorrectSetup(context, "dataSource", "h2DataSource", "derbyDataSource");
58-
context.close();
56+
assertCorrectSetup("jdbc-config.xml", "dataSource", "h2DataSource", "derbyDataSource");
5957
}
6058

6159
@Test
62-
public void testCreateEmbeddedDatabaseAgain() throws Exception {
60+
public void createEmbeddedDatabaseAgain() throws Exception {
6361
// If Derby isn't cleaned up properly this will fail...
6462
Assume.group(TestGroup.LONG_RUNNING);
65-
66-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
67-
"org/springframework/jdbc/config/jdbc-config.xml");
68-
assertCorrectSetup(context, "derbyDataSource");
69-
context.close();
63+
assertCorrectSetup("jdbc-config.xml", "derbyDataSource");
7064
}
7165

7266
@Test
73-
public void testCreateWithResourcePattern() throws Exception {
74-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
75-
"org/springframework/jdbc/config/jdbc-config-pattern.xml");
76-
assertCorrectSetup(context, "dataSource");
77-
context.close();
67+
public void createWithResourcePattern() throws Exception {
68+
assertCorrectSetup("jdbc-config-pattern.xml", "dataSource");
7869
}
7970

8071
@Test
81-
public void testCreateWithEndings() throws Exception {
82-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
83-
"org/springframework/jdbc/config/jdbc-initialize-endings-config.xml");
84-
assertCorrectSetup(context, 2, "dataSource");
85-
context.close();
72+
public void createWithEndings() throws Exception {
73+
assertCorrectSetupAndCloseContext("jdbc-initialize-endings-config.xml", 2, "dataSource");
8674
}
8775

8876
@Test
89-
public void testCreateWithEndingsNested() throws Exception {
90-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
91-
"org/springframework/jdbc/config/jdbc-initialize-endings-nested-config.xml");
92-
assertCorrectSetup(context, 2, "dataSource");
93-
context.close();
77+
public void createWithEndingsNested() throws Exception {
78+
assertCorrectSetupAndCloseContext("jdbc-initialize-endings-nested-config.xml", 2, "dataSource");
9479
}
9580

9681
@Test
97-
public void testCreateAndDestroy() throws Exception {
98-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
99-
"org/springframework/jdbc/config/jdbc-destroy-config.xml");
82+
public void createAndDestroy() throws Exception {
83+
ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
10084
try {
10185
DataSource dataSource = context.getBean(DataSource.class);
10286
JdbcTemplate template = new JdbcTemplate(dataSource);
103-
assertEquals(1, template.queryForInt("select count(*) from T_TEST"));
87+
assertNumRowsInTestTable(template, 1);
10488
context.getBean(DataSourceInitializer.class).destroy();
10589
expected.expect(BadSqlGrammarException.class); // Table has been dropped
106-
assertEquals(1, template.queryForInt("select count(*) from T_TEST"));
90+
assertNumRowsInTestTable(template, 1);
91+
}
92+
finally {
93+
context.close();
94+
}
95+
}
96+
97+
@Test
98+
public void createAndDestroyNestedWithHsql() throws Exception {
99+
ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml");
100+
try {
101+
DataSource dataSource = context.getBean(DataSource.class);
102+
JdbcTemplate template = new JdbcTemplate(dataSource);
103+
assertNumRowsInTestTable(template, 1);
104+
context.getBean(EmbeddedDatabaseFactoryBean.class).destroy();
105+
expected.expect(BadSqlGrammarException.class); // Table has been dropped
106+
assertNumRowsInTestTable(template, 1);
107107
}
108108
finally {
109109
context.close();
110110
}
111111
}
112112

113113
@Test
114-
public void testCreateAndDestroyNested() throws Exception {
115-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
116-
"org/springframework/jdbc/config/jdbc-destroy-nested-config.xml");
114+
public void createAndDestroyNestedWithH2() throws Exception {
115+
ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config-h2.xml");
117116
try {
118117
DataSource dataSource = context.getBean(DataSource.class);
119118
JdbcTemplate template = new JdbcTemplate(dataSource);
120-
assertEquals(1, template.queryForInt("select count(*) from T_TEST"));
119+
assertNumRowsInTestTable(template, 1);
121120
context.getBean(EmbeddedDatabaseFactoryBean.class).destroy();
122121
expected.expect(BadSqlGrammarException.class); // Table has been dropped
123-
assertEquals(1, template.queryForInt("select count(*) from T_TEST"));
122+
assertNumRowsInTestTable(template, 1);
124123
}
125124
finally {
126125
context.close();
127126
}
128127
}
129128

130129
@Test
131-
public void testMultipleDataSourcesHaveDifferentDatabaseNames() throws Exception {
130+
public void multipleDataSourcesHaveDifferentDatabaseNames() throws Exception {
132131
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
133132
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new ClassPathResource(
134-
"org/springframework/jdbc/config/jdbc-config-multiple-datasources.xml"));
133+
"jdbc-config-multiple-datasources.xml", getClass()));
135134
assertBeanPropertyValueOf("databaseName", "firstDataSource", factory);
136135
assertBeanPropertyValueOf("databaseName", "secondDataSource", factory);
137136
}
138137

138+
private ClassPathXmlApplicationContext context(String file) {
139+
return new ClassPathXmlApplicationContext(file, getClass());
140+
}
141+
139142
private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) {
140143
BeanDefinition bean = factory.getBeanDefinition(expected);
141144
PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName);
142145
assertThat(value, is(notNullValue()));
143146
assertThat(value.getValue().toString(), is(expected));
144147
}
145148

146-
private void assertCorrectSetup(ConfigurableApplicationContext context, String... dataSources) {
147-
assertCorrectSetup(context, 1, dataSources);
149+
private void assertNumRowsInTestTable(JdbcTemplate template, int count) {
150+
assertEquals(count, template.queryForObject("select count(*) from T_TEST", Integer.class).intValue());
148151
}
149152

150-
private void assertCorrectSetup(ConfigurableApplicationContext context, int count, String... dataSources) {
153+
private void assertCorrectSetup(String file, String... dataSources) {
154+
assertCorrectSetupAndCloseContext(file, 1, dataSources);
155+
}
156+
157+
private void assertCorrectSetupAndCloseContext(String file, int count, String... dataSources) {
158+
ConfigurableApplicationContext context = context(file);
151159
try {
152160
for (String dataSourceName : dataSources) {
153161
DataSource dataSource = context.getBean(dataSourceName, DataSource.class);
154162
JdbcTemplate template = new JdbcTemplate(dataSource);
155-
assertEquals(count, template.queryForInt("select count(*) from T_TEST"));
163+
assertNumRowsInTestTable(template, count);
156164
}
157165
}
158166
finally {
159167
context.close();
160168
}
161-
162169
}
163170

164171
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
7+
8+
<jdbc:embedded-database id="dataSource" type="H2">
9+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" execution="INIT"/>
10+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data.sql" execution="INIT"/>
11+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-drops.sql" execution="DESTROY"/>
12+
</jdbc:embedded-database>
13+
14+
</beans>

0 commit comments

Comments
 (0)