Fix command DevTools uses to shut down in-memory Derby DB
Closes gh-17099
This commit is contained in:
parent
a243b56ade
commit
95d0e26c35
|
@ -29,6 +29,11 @@
|
|||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
|
@ -121,11 +126,6 @@
|
|||
<artifactId>spring-hateoas</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>
|
||||
|
|
|
@ -17,13 +17,17 @@
|
|||
package org.springframework.boot.devtools.autoconfigure;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.derby.jdbc.EmbeddedDriver;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
|
@ -94,40 +98,53 @@ public class DevToolsDataSourceAutoConfiguration {
|
|||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
if (dataSourceRequiresShutdown()) {
|
||||
try (Connection connection = this.dataSource.getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("SHUTDOWN");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean dataSourceRequiresShutdown() {
|
||||
for (InMemoryDatabase inMemoryDatabase : InMemoryDatabase.values()) {
|
||||
if (inMemoryDatabase.matches(this.dataSourceProperties)) {
|
||||
return true;
|
||||
inMemoryDatabase.shutdown(this.dataSource);
|
||||
return;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private enum InMemoryDatabase {
|
||||
|
||||
DERBY(null, "org.apache.derby.jdbc.EmbeddedDriver"),
|
||||
DERBY(null, new HashSet<>(Arrays.asList("org.apache.derby.jdbc.EmbeddedDriver")), (dataSource) -> {
|
||||
String url = dataSource.getConnection().getMetaData().getURL();
|
||||
try {
|
||||
new EmbeddedDriver().connect(url + ";drop=true", new Properties());
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
if (!"08006".equals(ex.getSQLState())) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
H2("jdbc:h2:mem:", "org.h2.Driver", "org.h2.jdbcx.JdbcDataSource"),
|
||||
H2("jdbc:h2:mem:", new HashSet<>(Arrays.asList("org.h2.Driver", "org.h2.jdbcx.JdbcDataSource"))),
|
||||
|
||||
HSQLDB("jdbc:hsqldb:mem:", "org.hsqldb.jdbcDriver", "org.hsqldb.jdbc.JDBCDriver",
|
||||
"org.hsqldb.jdbc.pool.JDBCXADataSource");
|
||||
HSQLDB("jdbc:hsqldb:mem:", new HashSet<>(Arrays.asList("org.hsqldb.jdbcDriver",
|
||||
"org.hsqldb.jdbc.JDBCDriver", "org.hsqldb.jdbc.pool.JDBCXADataSource")));
|
||||
|
||||
private final String urlPrefix;
|
||||
|
||||
private final ShutdownHandler shutdownHandler;
|
||||
|
||||
private final Set<String> driverClassNames;
|
||||
|
||||
InMemoryDatabase(String urlPrefix, String... driverClassNames) {
|
||||
InMemoryDatabase(String urlPrefix, Set<String> driverClassNames) {
|
||||
this(urlPrefix, driverClassNames, (dataSource) -> {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("SHUTDOWN");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
InMemoryDatabase(String urlPrefix, Set<String> driverClassNames, ShutdownHandler shutdownHandler) {
|
||||
this.urlPrefix = urlPrefix;
|
||||
this.driverClassNames = new HashSet<>(Arrays.asList(driverClassNames));
|
||||
this.driverClassNames = driverClassNames;
|
||||
this.shutdownHandler = shutdownHandler;
|
||||
}
|
||||
|
||||
boolean matches(DataSourceProperties properties) {
|
||||
|
@ -136,6 +153,17 @@ public class DevToolsDataSourceAutoConfiguration {
|
|||
&& this.driverClassNames.contains(properties.determineDriverClassName());
|
||||
}
|
||||
|
||||
void shutdown(DataSource dataSource) throws SQLException {
|
||||
this.shutdownHandler.shutdown(dataSource);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface ShutdownHandler {
|
||||
|
||||
void shutdown(DataSource dataSource) throws SQLException;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,16 +18,21 @@ package org.springframework.boot.devtools.autoconfigure;
|
|||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.derby.jdbc.EmbeddedDriver;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
@ -115,10 +120,15 @@ public class DevToolsPooledDataSourceAutoConfigurationTests extends AbstractDevT
|
|||
@Test
|
||||
public void inMemoryDerbyIsShutdown() throws SQLException {
|
||||
ConfigurableApplicationContext context = createContext("org.apache.derby.jdbc.EmbeddedDriver",
|
||||
"jdbc:derby:memory:test", DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class);
|
||||
Statement statement = configureDataSourceBehavior(context.getBean(DataSource.class));
|
||||
"jdbc:derby:memory:test;create=true", DataSourceAutoConfiguration.class,
|
||||
DataSourceSpyConfiguration.class);
|
||||
JdbcTemplate jdbc = new JdbcTemplate(context.getBean(DataSource.class));
|
||||
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
|
||||
context.close();
|
||||
verify(statement, times(1)).execute("SHUTDOWN");
|
||||
// Connect should fail as DB no longer exists
|
||||
assertThatExceptionOfType(SQLException.class)
|
||||
.isThrownBy(() -> new EmbeddedDriver().connect("jdbc:derby:memory:test", new Properties()))
|
||||
.satisfies((ex) -> assertThat(ex.getSQLState()).isEqualTo("XJ004"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue