Skip to content

Commit 953df7c

Browse files
committed
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
2 parents f32c66d + 24d5209 commit 953df7c

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/LiquibaseEndpoint.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
* {@link Endpoint} to expose liquibase info.
3939
*
4040
* @author Eddú Meléndez
41+
* @author Dmitrii Sergeev
4142
* @since 1.3.0
4243
*/
4344
@ConfigurationProperties(prefix = "endpoints.liquibase")
@@ -65,9 +66,9 @@ public List<LiquibaseReport> invoke() {
6566
DataSource dataSource = entry.getValue().getDataSource();
6667
JdbcConnection connection = new JdbcConnection(
6768
dataSource.getConnection());
69+
Database database = null;
6870
try {
69-
Database database = factory
70-
.findCorrectDatabaseImplementation(connection);
71+
database = factory.findCorrectDatabaseImplementation(connection);
7172
String defaultSchema = entry.getValue().getDefaultSchema();
7273
if (StringUtils.hasText(defaultSchema)) {
7374
database.setDefaultSchemaName(defaultSchema);
@@ -76,7 +77,12 @@ public List<LiquibaseReport> invoke() {
7677
service.queryDatabaseChangeLogTable(database)));
7778
}
7879
finally {
79-
connection.close();
80+
if (database != null) {
81+
database.close();
82+
}
83+
else {
84+
connection.close();
85+
}
8086
}
8187
}
8288
catch (Exception ex) {

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/LiquibaseEndpointTests.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,15 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19+
import java.sql.Connection;
20+
import java.sql.SQLException;
21+
22+
import javax.sql.DataSource;
23+
1924
import liquibase.integration.spring.SpringLiquibase;
2025
import org.junit.Test;
2126

2227
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
23-
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
2428
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
2529
import org.springframework.boot.test.util.EnvironmentTestUtils;
2630
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -44,7 +48,20 @@ public LiquibaseEndpointTests() {
4448

4549
@Test
4650
public void invoke() throws Exception {
51+
DataSource dataSource = this.context.getBean(DataSource.class);
52+
assertThat(getAutoCommit(dataSource)).isTrue();
4753
assertThat(getEndpointBean().invoke()).hasSize(1);
54+
assertThat(getAutoCommit(dataSource)).isTrue();
55+
}
56+
57+
private boolean getAutoCommit(DataSource dataSource) throws SQLException {
58+
Connection connection = dataSource.getConnection();
59+
try {
60+
return connection.getAutoCommit();
61+
}
62+
finally {
63+
connection.close();
64+
}
4865
}
4966

5067
@Test
@@ -55,13 +72,13 @@ public void invokeWithCustomSchema() throws Exception {
5572
"liquibase.default-schema=CUSTOMSCHEMA",
5673
"spring.datasource.generate-unique-name=true",
5774
"spring.datasource.schema=classpath:/db/create-custom-schema.sql");
58-
this.context.register(CustomSchemaConfig.class);
75+
this.context.register(Config.class);
5976
this.context.refresh();
6077
assertThat(getEndpointBean().invoke()).hasSize(1);
6178
}
6279

6380
@Configuration
64-
@Import({ EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class })
81+
@Import({ DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class })
6582
public static class Config {
6683

6784
private final SpringLiquibase liquibase;
@@ -77,21 +94,4 @@ public LiquibaseEndpoint endpoint() {
7794

7895
}
7996

80-
@Configuration
81-
@Import({ DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class })
82-
public static class CustomSchemaConfig {
83-
84-
private final SpringLiquibase liquibase;
85-
86-
public CustomSchemaConfig(SpringLiquibase liquibase) {
87-
this.liquibase = liquibase;
88-
}
89-
90-
@Bean
91-
public LiquibaseEndpoint endpoint() {
92-
return new LiquibaseEndpoint(this.liquibase);
93-
}
94-
95-
}
96-
9797
}

0 commit comments

Comments
 (0)