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:
Andy Wilkinson 2018-06-25 11:07:47 +01:00
commit 953df7cf86
2 changed files with 31 additions and 25 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * {@link Endpoint} to expose liquibase info.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Dmitrii Sergeev
* @since 1.3.0 * @since 1.3.0
*/ */
@ConfigurationProperties(prefix = "endpoints.liquibase") @ConfigurationProperties(prefix = "endpoints.liquibase")
@ -65,9 +66,9 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
DataSource dataSource = entry.getValue().getDataSource(); DataSource dataSource = entry.getValue().getDataSource();
JdbcConnection connection = new JdbcConnection( JdbcConnection connection = new JdbcConnection(
dataSource.getConnection()); dataSource.getConnection());
Database database = null;
try { try {
Database database = factory database = factory.findCorrectDatabaseImplementation(connection);
.findCorrectDatabaseImplementation(connection);
String defaultSchema = entry.getValue().getDefaultSchema(); String defaultSchema = entry.getValue().getDefaultSchema();
if (StringUtils.hasText(defaultSchema)) { if (StringUtils.hasText(defaultSchema)) {
database.setDefaultSchemaName(defaultSchema); database.setDefaultSchemaName(defaultSchema);
@ -76,7 +77,12 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
service.queryDatabaseChangeLogTable(database))); service.queryDatabaseChangeLogTable(database)));
} }
finally { finally {
connection.close(); if (database != null) {
database.close();
}
else {
connection.close();
}
} }
} }
catch (Exception ex) { catch (Exception ex) {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,11 +16,15 @@
package org.springframework.boot.actuate.endpoint; package org.springframework.boot.actuate.endpoint;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase; import liquibase.integration.spring.SpringLiquibase;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 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.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -44,7 +48,20 @@ public class LiquibaseEndpointTests extends AbstractEndpointTests<LiquibaseEndpo
@Test @Test
public void invoke() throws Exception { public void invoke() throws Exception {
DataSource dataSource = this.context.getBean(DataSource.class);
assertThat(getAutoCommit(dataSource)).isTrue();
assertThat(getEndpointBean().invoke()).hasSize(1); 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 @Test
@ -55,13 +72,13 @@ public class LiquibaseEndpointTests extends AbstractEndpointTests<LiquibaseEndpo
"liquibase.default-schema=CUSTOMSCHEMA", "liquibase.default-schema=CUSTOMSCHEMA",
"spring.datasource.generate-unique-name=true", "spring.datasource.generate-unique-name=true",
"spring.datasource.schema=classpath:/db/create-custom-schema.sql"); "spring.datasource.schema=classpath:/db/create-custom-schema.sql");
this.context.register(CustomSchemaConfig.class); this.context.register(Config.class);
this.context.refresh(); this.context.refresh();
assertThat(getEndpointBean().invoke()).hasSize(1); assertThat(getEndpointBean().invoke()).hasSize(1);
} }
@Configuration @Configuration
@Import({ EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class }) @Import({ DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class })
public static class Config { public static class Config {
private final SpringLiquibase liquibase; 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);
}
}
} }