mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Merge branch '2.1.x'
Closes gh-17104
This commit is contained in:
commit
c11f25bf8b
@ -34,6 +34,11 @@
|
||||
<artifactId>jakarta.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>
|
||||
@ -140,11 +145,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,10 +18,13 @@ package org.springframework.boot.devtools.autoconfigure;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
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.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -29,7 +32,10 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
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;
|
||||
@ -116,12 +122,16 @@ class DevToolsPooledDataSourceAutoConfigurationTests extends AbstractDevToolsDat
|
||||
|
||||
@Test
|
||||
void inMemoryDerbyIsShutdown() throws Exception {
|
||||
ConfigurableApplicationContext configurableApplicationContext = getContext(
|
||||
() -> createContext("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test",
|
||||
ConfigurableApplicationContext context = getContext(
|
||||
() -> createContext("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:test;create=true",
|
||||
DataSourceAutoConfiguration.class, DataSourceSpyConfiguration.class));
|
||||
Statement statement = configureDataSourceBehavior(configurableApplicationContext.getBean(DataSource.class));
|
||||
configurableApplicationContext.close();
|
||||
verify(statement, times(1)).execute("SHUTDOWN");
|
||||
JdbcTemplate jdbc = new JdbcTemplate(context.getBean(DataSource.class));
|
||||
jdbc.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1");
|
||||
context.close();
|
||||
// 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
Block a user