Don't shut down "in-memory" DB running as a server on DevTools restart

Closes gh-8702
pull/8929/head
Andy Wilkinson 8 years ago
parent 5dad7182db
commit 60505a4fae

@ -112,6 +112,17 @@
<artifactId>spring-webmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>${derby.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
@ -122,6 +133,11 @@
<artifactId>websocket-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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,7 +42,6 @@ import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -85,12 +84,6 @@ public class DevToolsDataSourceAutoConfiguration {
static final class NonEmbeddedInMemoryDatabaseShutdownExecutor
implements DisposableBean {
private static final Set<String> IN_MEMORY_DRIVER_CLASS_NAMES = new HashSet<String>(
Arrays.asList("org.apache.derby.jdbc.EmbeddedDriver", "org.h2.Driver",
"org.h2.jdbcx.JdbcDataSource", "org.hsqldb.jdbcDriver",
"org.hsqldb.jdbc.JDBCDriver",
"org.hsqldb.jdbc.pool.JDBCXADataSource"));
private final DataSource dataSource;
private final DataSourceProperties dataSourceProperties;
@ -109,9 +102,42 @@ public class DevToolsDataSourceAutoConfiguration {
}
private boolean dataSourceRequiresShutdown() {
return IN_MEMORY_DRIVER_CLASS_NAMES
.contains(this.dataSourceProperties.determineDriverClassName())
&& (!(this.dataSource instanceof EmbeddedDatabase));
for (InMemoryDatabase inMemoryDatabase : InMemoryDatabase.values()) {
if (inMemoryDatabase.matches(this.dataSourceProperties)) {
return true;
}
}
return false;
}
private static enum InMemoryDatabase {
DERBY(null, "org.apache.derby.jdbc.EmbeddedDriver"),
H2("jdbc:h2:mem:", "org.h2.Driver", "org.h2.jdbcx.JdbcDataSource"),
HQSQLDB("jdbc:hsqldb:mem:", "org.hsqldb.jdbcDriver",
"org.hsqldb.jdbc.JDBCDriver",
"org.hsqldb.jdbc.pool.JDBCXADataSource");
private final String urlPrefix;
private final Set<String> driverClassNames;
InMemoryDatabase(String urlPrefix, String... driverClassNames) {
this.urlPrefix = urlPrefix;
this.driverClassNames = new HashSet<String>(
Arrays.asList(driverClassNames));
}
boolean matches(DataSourceProperties properties) {
String url = properties.getUrl();
return (url == null || this.urlPrefix == null
|| url.startsWith(this.urlPrefix))
&& this.driverClassNames
.contains(properties.determineDriverClassName());
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@ -95,8 +95,17 @@ public abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
return statement;
}
protected final ConfigurableApplicationContext createContext(Class<?>... classes) {
return this.createContext(null, classes);
}
protected final ConfigurableApplicationContext createContext(String driverClassName,
Class<?>... classes) {
return this.createContext(driverClassName, null, classes);
}
protected final ConfigurableApplicationContext createContext(String driverClassName,
String url, Class<?>... classes) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(classes);
context.register(DevToolsDataSourceAutoConfiguration.class);
@ -104,14 +113,13 @@ public abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
EnvironmentTestUtils.addEnvironment(context,
"spring.datasource.driver-class-name:" + driverClassName);
}
if (url != null) {
EnvironmentTestUtils.addEnvironment(context, "spring.datasource.url:" + url);
}
context.refresh();
return context;
}
protected final ConfigurableApplicationContext createContext(Class<?>... classes) {
return this.createContext(null, classes);
}
@Configuration
static class SingleDataSourceConfiguration {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@ -57,4 +57,70 @@ public class DevToolsPooledDataSourceAutoConfigurationTests
verify(statement, times(0)).execute("SHUTDOWN");
}
@Test
public void h2ServerIsNotShutdown() throws SQLException {
ConfigurableApplicationContext context = createContext("org.h2.Driver",
"jdbc:h2:hsql://localhost", DataSourceAutoConfiguration.class,
DataSourceSpyConfiguration.class);
Statement statement = configureDataSourceBehaviour(
context.getBean(DataSource.class));
context.close();
verify(statement, times(0)).execute("SHUTDOWN");
}
@Test
public void inMemoryh2IsShutdown() throws SQLException {
ConfigurableApplicationContext context = createContext("org.h2.Driver",
"jdbc:h2:mem:test", DataSourceAutoConfiguration.class,
DataSourceSpyConfiguration.class);
Statement statement = configureDataSourceBehaviour(
context.getBean(DataSource.class));
context.close();
verify(statement, times(1)).execute("SHUTDOWN");
}
@Test
public void hsqlServerIsNotShutdown() throws SQLException {
ConfigurableApplicationContext context = createContext("org.hsqldb.jdbcDriver",
"jdbc:hsqldb:hsql://localhost", DataSourceAutoConfiguration.class,
DataSourceSpyConfiguration.class);
Statement statement = configureDataSourceBehaviour(
context.getBean(DataSource.class));
context.close();
verify(statement, times(0)).execute("SHUTDOWN");
}
@Test
public void inMemoryHsqlIsShutdown() throws SQLException {
ConfigurableApplicationContext context = createContext("org.hsqldb.jdbcDriver",
"jdbc:hsqldb:mem:test", DataSourceAutoConfiguration.class,
DataSourceSpyConfiguration.class);
Statement statement = configureDataSourceBehaviour(
context.getBean(DataSource.class));
context.close();
verify(statement, times(1)).execute("SHUTDOWN");
}
@Test
public void derbyClientIsNotShutdown() throws SQLException {
ConfigurableApplicationContext context = createContext(
"org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class);
Statement statement = configureDataSourceBehaviour(
context.getBean(DataSource.class));
context.close();
verify(statement, times(0)).execute("SHUTDOWN");
}
@Test
public void inMemoryDerbyIsShutdown() throws SQLException {
ConfigurableApplicationContext context = createContext(
"org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test",
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class);
Statement statement = configureDataSourceBehaviour(
context.getBean(DataSource.class));
context.close();
verify(statement, times(1)).execute("SHUTDOWN");
}
}

Loading…
Cancel
Save