From 8aefcb9a550886fe93b11d7e276f2236840944d3 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 19 Mar 2014 01:22:46 +0100 Subject: [PATCH] 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 XML namespace, EmbeddedDatabaseBuilder, EmbeddedDatabaseFactory, and EmbeddedDatabaseFactoryBean). Issue: SPR-11573 --- .../H2EmbeddedDatabaseConfigurer.java | 5 +- .../config/JdbcNamespaceIntegrationTests.java | 99 ++++++++++--------- .../config/jdbc-destroy-nested-config-h2.xml | 14 +++ 3 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-destroy-nested-config-h2.xml diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java index c79a7a4dc83..57c6000a6df 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java @@ -26,6 +26,7 @@ import org.springframework.util.ClassUtils; * * @author Oliver Gierke * @author Juergen Hoeller + * @author Sam Brannen * @since 3.0 */ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigurer { @@ -36,7 +37,7 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu /** - * Get the singleton {@link H2EmbeddedDatabaseConfigurer} instance. + * Get the singleton {@code H2EmbeddedDatabaseConfigurer} instance. * @return the configurer * @throws ClassNotFoundException if H2 is not on the classpath */ @@ -57,7 +58,7 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu @Override public void configureConnectionProperties(ConnectionProperties properties, String databaseName) { properties.setDriverClass(this.driverClass); - properties.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", databaseName)); + properties.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false", databaseName)); properties.setUsername("sa"); properties.setPassword(""); } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java index c789afa3cd1..396418b94c3 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,68 +42,52 @@ import static org.junit.Assert.*; * @author Dave Syer * @author Juergen Hoeller * @author Chris Beams + * @author Sam Brannen */ public class JdbcNamespaceIntegrationTests { @Rule public ExpectedException expected = ExpectedException.none(); - @Test - public void testCreateEmbeddedDatabase() throws Exception { - Assume.group(TestGroup.LONG_RUNNING); - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-config.xml"); - assertCorrectSetup(context, "dataSource", "h2DataSource", "derbyDataSource"); - context.close(); + @Test + public void createEmbeddedDatabase() throws Exception { + Assume.group(TestGroup.LONG_RUNNING); + assertCorrectSetup("jdbc-config.xml", "dataSource", "h2DataSource", "derbyDataSource"); } @Test - public void testCreateEmbeddedDatabaseAgain() throws Exception { + public void createEmbeddedDatabaseAgain() throws Exception { // If Derby isn't cleaned up properly this will fail... Assume.group(TestGroup.LONG_RUNNING); - - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-config.xml"); - assertCorrectSetup(context, "derbyDataSource"); - context.close(); + assertCorrectSetup("jdbc-config.xml", "derbyDataSource"); } @Test - public void testCreateWithResourcePattern() throws Exception { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-config-pattern.xml"); - assertCorrectSetup(context, "dataSource"); - context.close(); + public void createWithResourcePattern() throws Exception { + assertCorrectSetup("jdbc-config-pattern.xml", "dataSource"); } @Test - public void testCreateWithEndings() throws Exception { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-initialize-endings-config.xml"); - assertCorrectSetup(context, 2, "dataSource"); - context.close(); + public void createWithEndings() throws Exception { + assertCorrectSetupAndCloseContext("jdbc-initialize-endings-config.xml", 2, "dataSource"); } @Test - public void testCreateWithEndingsNested() throws Exception { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-initialize-endings-nested-config.xml"); - assertCorrectSetup(context, 2, "dataSource"); - context.close(); + public void createWithEndingsNested() throws Exception { + assertCorrectSetupAndCloseContext("jdbc-initialize-endings-nested-config.xml", 2, "dataSource"); } @Test - public void testCreateAndDestroy() throws Exception { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-destroy-config.xml"); + public void createAndDestroy() throws Exception { + ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml"); try { DataSource dataSource = context.getBean(DataSource.class); JdbcTemplate template = new JdbcTemplate(dataSource); - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); + assertNumRowsInTestTable(template, 1); context.getBean(DataSourceInitializer.class).destroy(); expected.expect(BadSqlGrammarException.class); // Table has been dropped - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); + assertNumRowsInTestTable(template, 1); } finally { context.close(); @@ -111,16 +95,15 @@ public class JdbcNamespaceIntegrationTests { } @Test - public void testCreateAndDestroyNested() throws Exception { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "org/springframework/jdbc/config/jdbc-destroy-nested-config.xml"); + public void createAndDestroyNestedWithHsql() throws Exception { + ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml"); try { DataSource dataSource = context.getBean(DataSource.class); JdbcTemplate template = new JdbcTemplate(dataSource); - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); + assertNumRowsInTestTable(template, 1); context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); expected.expect(BadSqlGrammarException.class); // Table has been dropped - assertEquals(1, template.queryForInt("select count(*) from T_TEST")); + assertNumRowsInTestTable(template, 1); } finally { context.close(); @@ -128,14 +111,34 @@ public class JdbcNamespaceIntegrationTests { } @Test - public void testMultipleDataSourcesHaveDifferentDatabaseNames() throws Exception { + public void createAndDestroyNestedWithH2() throws Exception { + ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config-h2.xml"); + try { + DataSource dataSource = context.getBean(DataSource.class); + JdbcTemplate template = new JdbcTemplate(dataSource); + assertNumRowsInTestTable(template, 1); + context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); + expected.expect(BadSqlGrammarException.class); // Table has been dropped + assertNumRowsInTestTable(template, 1); + } + finally { + context.close(); + } + } + + @Test + public void multipleDataSourcesHaveDifferentDatabaseNames() throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(factory).loadBeanDefinitions(new ClassPathResource( - "org/springframework/jdbc/config/jdbc-config-multiple-datasources.xml")); + "jdbc-config-multiple-datasources.xml", getClass())); assertBeanPropertyValueOf("databaseName", "firstDataSource", factory); assertBeanPropertyValueOf("databaseName", "secondDataSource", factory); } + private ClassPathXmlApplicationContext context(String file) { + return new ClassPathXmlApplicationContext(file, getClass()); + } + private void assertBeanPropertyValueOf(String propertyName, String expected, DefaultListableBeanFactory factory) { BeanDefinition bean = factory.getBeanDefinition(expected); PropertyValue value = bean.getPropertyValues().getPropertyValue(propertyName); @@ -143,22 +146,26 @@ public class JdbcNamespaceIntegrationTests { assertThat(value.getValue().toString(), is(expected)); } - private void assertCorrectSetup(ConfigurableApplicationContext context, String... dataSources) { - assertCorrectSetup(context, 1, dataSources); + private void assertNumRowsInTestTable(JdbcTemplate template, int count) { + assertEquals(count, template.queryForObject("select count(*) from T_TEST", Integer.class).intValue()); } - private void assertCorrectSetup(ConfigurableApplicationContext context, int count, String... dataSources) { + private void assertCorrectSetup(String file, String... dataSources) { + assertCorrectSetupAndCloseContext(file, 1, dataSources); + } + + private void assertCorrectSetupAndCloseContext(String file, int count, String... dataSources) { + ConfigurableApplicationContext context = context(file); try { for (String dataSourceName : dataSources) { DataSource dataSource = context.getBean(dataSourceName, DataSource.class); JdbcTemplate template = new JdbcTemplate(dataSource); - assertEquals(count, template.queryForInt("select count(*) from T_TEST")); + assertNumRowsInTestTable(template, count); } } finally { context.close(); } - } } diff --git a/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-destroy-nested-config-h2.xml b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-destroy-nested-config-h2.xml new file mode 100644 index 00000000000..bbea98e940b --- /dev/null +++ b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-destroy-nested-config-h2.xml @@ -0,0 +1,14 @@ + + + + + + + + + +