mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Merge pull request #13559 from Dmitry Sergeyev
* gh-13559: Polish “Close Database to reset Connection's auto commit property” Close Database to reset Connection's auto commit property
This commit is contained in:
commit
953df7cf86
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
|
||||
* {@link Endpoint} to expose liquibase info.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Dmitrii Sergeev
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "endpoints.liquibase")
|
||||
@ -65,9 +66,9 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
|
||||
DataSource dataSource = entry.getValue().getDataSource();
|
||||
JdbcConnection connection = new JdbcConnection(
|
||||
dataSource.getConnection());
|
||||
Database database = null;
|
||||
try {
|
||||
Database database = factory
|
||||
.findCorrectDatabaseImplementation(connection);
|
||||
database = factory.findCorrectDatabaseImplementation(connection);
|
||||
String defaultSchema = entry.getValue().getDefaultSchema();
|
||||
if (StringUtils.hasText(defaultSchema)) {
|
||||
database.setDefaultSchemaName(defaultSchema);
|
||||
@ -76,7 +77,12 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
|
||||
service.queryDatabaseChangeLogTable(database)));
|
||||
}
|
||||
finally {
|
||||
connection.close();
|
||||
if (database != null) {
|
||||
database.close();
|
||||
}
|
||||
else {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
@ -16,11 +16,15 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import liquibase.integration.spring.SpringLiquibase;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@ -44,7 +48,20 @@ public class LiquibaseEndpointTests extends AbstractEndpointTests<LiquibaseEndpo
|
||||
|
||||
@Test
|
||||
public void invoke() throws Exception {
|
||||
DataSource dataSource = this.context.getBean(DataSource.class);
|
||||
assertThat(getAutoCommit(dataSource)).isTrue();
|
||||
assertThat(getEndpointBean().invoke()).hasSize(1);
|
||||
assertThat(getAutoCommit(dataSource)).isTrue();
|
||||
}
|
||||
|
||||
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
|
||||
Connection connection = dataSource.getConnection();
|
||||
try {
|
||||
return connection.getAutoCommit();
|
||||
}
|
||||
finally {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -55,13 +72,13 @@ public class LiquibaseEndpointTests extends AbstractEndpointTests<LiquibaseEndpo
|
||||
"liquibase.default-schema=CUSTOMSCHEMA",
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.datasource.schema=classpath:/db/create-custom-schema.sql");
|
||||
this.context.register(CustomSchemaConfig.class);
|
||||
this.context.register(Config.class);
|
||||
this.context.refresh();
|
||||
assertThat(getEndpointBean().invoke()).hasSize(1);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class })
|
||||
@Import({ DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class })
|
||||
public static class Config {
|
||||
|
||||
private final SpringLiquibase liquibase;
|
||||
@ -77,21 +94,4 @@ public class LiquibaseEndpointTests extends AbstractEndpointTests<LiquibaseEndpo
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class })
|
||||
public static class CustomSchemaConfig {
|
||||
|
||||
private final SpringLiquibase liquibase;
|
||||
|
||||
public CustomSchemaConfig(SpringLiquibase liquibase) {
|
||||
this.liquibase = liquibase;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LiquibaseEndpoint endpoint() {
|
||||
return new LiquibaseEndpoint(this.liquibase);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user