Skip to content

Commit 24d5209

Browse files
committed
Polish “Close Database to reset Connection's auto commit property”
Closes gh-13559
1 parent 3498a91 commit 24d5209

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

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

Lines changed: 4 additions & 1 deletion
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.
@@ -80,6 +80,9 @@ public List<LiquibaseReport> invoke() {
8080
if (database != null) {
8181
database.close();
8282
}
83+
else {
84+
connection.close();
85+
}
8386
}
8487
}
8588
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)