Skip to content

Commit 0fedf8d

Browse files
committed
Keep Liquibase-specific DataSource open for use by LiquibaseEndpoint
Closes gh-13832
1 parent d4ec45e commit 0fedf8d

File tree

4 files changed

+156
-29
lines changed

4 files changed

+156
-29
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/liquibase/LiquibaseEndpointAutoConfiguration.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818

1919
import liquibase.integration.spring.SpringLiquibase;
2020

21+
import org.springframework.beans.BeansException;
22+
import org.springframework.beans.factory.config.BeanPostProcessor;
2123
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
2224
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint;
2325
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2426
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2527
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2628
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2729
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
30+
import org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase;
2831
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
2932
import org.springframework.context.ApplicationContext;
3033
import org.springframework.context.annotation.Bean;
@@ -49,4 +52,29 @@ public LiquibaseEndpoint liquibaseEndpoint(ApplicationContext context) {
4952
return new LiquibaseEndpoint(context);
5053
}
5154

55+
@Bean
56+
@ConditionalOnBean(SpringLiquibase.class)
57+
@ConditionalOnEnabledEndpoint(endpoint = LiquibaseEndpoint.class)
58+
public static BeanPostProcessor preventDataSourceCloseBeanPostProcessor() {
59+
return new BeanPostProcessor() {
60+
61+
@Override
62+
public Object postProcessBeforeInitialization(Object bean, String beanName)
63+
throws BeansException {
64+
if (bean instanceof DataSourceClosingSpringLiquibase) {
65+
((DataSourceClosingSpringLiquibase) bean)
66+
.setCloseDataSourceOnceMigrated(false);
67+
}
68+
return bean;
69+
}
70+
71+
@Override
72+
public Object postProcessAfterInitialization(Object bean, String beanName)
73+
throws BeansException {
74+
return bean;
75+
}
76+
77+
};
78+
}
79+
5280
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/liquibase/LiquibaseEndpointAutoConfigurationTests.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.liquibase;
1818

19+
import liquibase.exception.LiquibaseException;
1920
import liquibase.integration.spring.SpringLiquibase;
2021
import org.junit.Test;
2122

2223
import org.springframework.boot.actuate.liquibase.LiquibaseEndpoint;
2324
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase;
2426
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2527
import org.springframework.context.annotation.Bean;
2628
import org.springframework.context.annotation.Configuration;
@@ -37,23 +39,48 @@ public class LiquibaseEndpointAutoConfigurationTests {
3739

3840
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
3941
.withConfiguration(
40-
AutoConfigurations.of(LiquibaseEndpointAutoConfiguration.class))
41-
.withUserConfiguration(LiquibaseConfiguration.class);
42+
AutoConfigurations.of(LiquibaseEndpointAutoConfiguration.class));
4243

4344
@Test
4445
public void runShouldHaveEndpointBean() {
45-
this.contextRunner.run(
46+
this.contextRunner.withUserConfiguration(LiquibaseConfiguration.class).run(
4647
(context) -> assertThat(context).hasSingleBean(LiquibaseEndpoint.class));
4748
}
4849

4950
@Test
5051
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() {
51-
this.contextRunner
52+
this.contextRunner.withUserConfiguration(LiquibaseConfiguration.class)
5253
.withPropertyValues("management.endpoint.liquibase.enabled:false")
5354
.run((context) -> assertThat(context)
5455
.doesNotHaveBean(LiquibaseEndpoint.class));
5556
}
5657

58+
@Test
59+
public void disablesCloseOfDataSourceWhenEndpointIsEnabled() {
60+
this.contextRunner
61+
.withUserConfiguration(DataSourceClosingLiquibaseConfiguration.class)
62+
.run((context) -> {
63+
assertThat(context).hasSingleBean(LiquibaseEndpoint.class);
64+
assertThat(context.getBean(DataSourceClosingSpringLiquibase.class))
65+
.hasFieldOrPropertyWithValue("closeDataSourceOnceMigrated",
66+
false);
67+
});
68+
}
69+
70+
@Test
71+
public void doesNotDisableCloseOfDataSourceWhenEndpointIsDisabled() {
72+
this.contextRunner
73+
.withUserConfiguration(DataSourceClosingLiquibaseConfiguration.class)
74+
.withPropertyValues("management.endpoint.liquibase.enabled:false")
75+
.run((context) -> {
76+
assertThat(context).doesNotHaveBean(LiquibaseEndpoint.class);
77+
DataSourceClosingSpringLiquibase bean = context
78+
.getBean(DataSourceClosingSpringLiquibase.class);
79+
assertThat(bean).hasFieldOrPropertyWithValue(
80+
"closeDataSourceOnceMigrated", true);
81+
});
82+
}
83+
5784
@Configuration
5885
static class LiquibaseConfiguration {
5986

@@ -64,4 +91,33 @@ public SpringLiquibase liquibase() {
6491

6592
}
6693

94+
@Configuration
95+
static class DataSourceClosingLiquibaseConfiguration {
96+
97+
@Bean
98+
public SpringLiquibase liquibase() {
99+
return new DataSourceClosingSpringLiquibase() {
100+
101+
private boolean propertiesSet = false;
102+
103+
@Override
104+
public void setCloseDataSourceOnceMigrated(
105+
boolean closeDataSourceOnceMigrated) {
106+
if (this.propertiesSet) {
107+
throw new IllegalStateException("setCloseDataSourceOnceMigrated "
108+
+ "invoked after afterPropertiesSet");
109+
}
110+
super.setCloseDataSourceOnceMigrated(closeDataSourceOnceMigrated);
111+
}
112+
113+
@Override
114+
public void afterPropertiesSet() throws LiquibaseException {
115+
this.propertiesSet = true;
116+
}
117+
118+
};
119+
}
120+
121+
}
122+
67123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.liquibase;
18+
19+
import java.lang.reflect.Method;
20+
21+
import javax.sql.DataSource;
22+
23+
import liquibase.exception.LiquibaseException;
24+
import liquibase.integration.spring.SpringLiquibase;
25+
26+
import org.springframework.beans.factory.DisposableBean;
27+
import org.springframework.util.ReflectionUtils;
28+
29+
/**
30+
* A custom {@link SpringLiquibase} extension that closes the underlying
31+
* {@link DataSource} once the database has been migrated.
32+
*
33+
* @author Andy Wilkinson
34+
* @since 2.0.6
35+
*/
36+
public class DataSourceClosingSpringLiquibase extends SpringLiquibase
37+
implements DisposableBean {
38+
39+
private volatile boolean closeDataSourceOnceMigrated = true;
40+
41+
public void setCloseDataSourceOnceMigrated(boolean closeDataSourceOnceMigrated) {
42+
this.closeDataSourceOnceMigrated = closeDataSourceOnceMigrated;
43+
}
44+
45+
@Override
46+
public void afterPropertiesSet() throws LiquibaseException {
47+
super.afterPropertiesSet();
48+
if (this.closeDataSourceOnceMigrated) {
49+
closeDataSource();
50+
}
51+
}
52+
53+
private void closeDataSource() {
54+
Class<?> dataSourceClass = getDataSource().getClass();
55+
Method closeMethod = ReflectionUtils.findMethod(dataSourceClass, "close");
56+
if (closeMethod != null) {
57+
ReflectionUtils.invokeMethod(closeMethod, getDataSource());
58+
}
59+
}
60+
61+
@Override
62+
public void destroy() throws Exception {
63+
if (!this.closeDataSourceOnceMigrated) {
64+
closeDataSource();
65+
}
66+
}
67+
68+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.liquibase;
1818

19-
import java.lang.reflect.Method;
2019
import java.util.Collections;
2120
import java.util.List;
2221
import java.util.function.Supplier;
@@ -26,7 +25,6 @@
2625
import javax.sql.DataSource;
2726

2827
import liquibase.change.DatabaseChange;
29-
import liquibase.exception.LiquibaseException;
3028
import liquibase.integration.spring.SpringLiquibase;
3129

3230
import org.springframework.beans.factory.ObjectProvider;
@@ -52,7 +50,6 @@
5250
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
5351
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
5452
import org.springframework.util.Assert;
55-
import org.springframework.util.ReflectionUtils;
5653

5754
/**
5855
* {@link EnableAutoConfiguration Auto-configuration} for Liquibase.
@@ -207,26 +204,4 @@ public LiquibaseJdbcOperationsDependencyConfiguration() {
207204

208205
}
209206

210-
/**
211-
* A custom {@link SpringLiquibase} extension that closes the underlying
212-
* {@link DataSource} once the database has been migrated.
213-
*/
214-
private static final class DataSourceClosingSpringLiquibase extends SpringLiquibase {
215-
216-
@Override
217-
public void afterPropertiesSet() throws LiquibaseException {
218-
super.afterPropertiesSet();
219-
closeDataSource();
220-
}
221-
222-
private void closeDataSource() {
223-
Class<?> dataSourceClass = getDataSource().getClass();
224-
Method closeMethod = ReflectionUtils.findMethod(dataSourceClass, "close");
225-
if (closeMethod != null) {
226-
ReflectionUtils.invokeMethod(closeMethod, getDataSource());
227-
}
228-
}
229-
230-
}
231-
232207
}

0 commit comments

Comments
 (0)