Skip to content

Commit 460318e

Browse files
vpavicsnicoll
authored andcommitted
Handle lazy connection proxy datasource in DataSourceBuilder
See gh-50271 Signed-off-by: Vedran Pavic <vedran@vedranpavic.com>
1 parent 96a36cf commit 460318e

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import org.springframework.beans.BeanUtils;
4343
import org.springframework.core.ResolvableType;
44+
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
4445
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
4546
import org.springframework.lang.Contract;
4647
import org.springframework.util.Assert;
@@ -241,6 +242,10 @@ public static DataSourceBuilder<?> derivedFrom(DataSource dataSource) {
241242
}
242243

243244
private static DataSource unwrap(DataSource dataSource) {
245+
if ((dataSource instanceof LazyConnectionDataSourceProxy dataSourceProxy)
246+
&& (dataSourceProxy.getTargetDataSource() != null)) {
247+
dataSource = dataSourceProxy.getTargetDataSource();
248+
}
244249
try {
245250
while (dataSource.isWrapperFor(DataSource.class)) {
246251
DataSource unwrapped = dataSource.unwrap(DataSource.class);

module/spring-boot-jdbc/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.vibur.dbcp.ViburDBCPDataSource;
4747

4848
import org.springframework.jdbc.datasource.AbstractDataSource;
49+
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
4950
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
5051
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
5152
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
@@ -61,6 +62,7 @@
6162
* @author Stephane Nicoll
6263
* @author Fabio Grassi
6364
* @author Phillip Webb
65+
* @author Vedran Pavic
6466
*/
6567
class DataSourceBuilderTests {
6668

@@ -427,6 +429,19 @@ void buildWhenDerivedFromWrappedDataSource() {
427429
assertThat(built.getJdbcUrl()).isEqualTo("jdbc:h2:test");
428430
}
429431

432+
@Test
433+
void buildWhenDerivedFromLazyConnectionDataSourceProxy() {
434+
HikariDataSource dataSource = new HikariDataSource();
435+
dataSource.setUsername("test");
436+
dataSource.setPassword("secret");
437+
dataSource.setJdbcUrl("jdbc:h2:test");
438+
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(new LazyConnectionDataSourceProxy(dataSource));
439+
HikariDataSource built = (HikariDataSource) builder.username("test2").password("secret2").build();
440+
assertThat(built.getUsername()).isEqualTo("test2");
441+
assertThat(built.getPassword()).isEqualTo("secret2");
442+
assertThat(built.getJdbcUrl()).isEqualTo("jdbc:h2:test");
443+
}
444+
430445
@Test // gh-26644
431446
void buildWhenDerivedFromExistingDatabaseWithTypeChange() {
432447
HikariDataSource dataSource = new HikariDataSource();

module/spring-boot-liquibase/src/test/java/org/springframework/boot/liquibase/autoconfigure/LiquibaseAutoConfigurationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.beans.factory.config.BeanDefinition;
5252
import org.springframework.boot.autoconfigure.AutoConfigurations;
5353
import org.springframework.boot.jdbc.DataSourceBuilder;
54+
import org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration;
5455
import org.springframework.boot.jdbc.autoconfigure.EmbeddedDataSourceConfiguration;
5556
import org.springframework.boot.jdbc.autoconfigure.JdbcConnectionDetails;
5657
import org.springframework.boot.jdbc.autoconfigure.JdbcTemplateAutoConfiguration;
@@ -90,6 +91,7 @@
9091
* @author Moritz Halbritter
9192
* @author Phillip Webb
9293
* @author Ahmed Ashour
94+
* @author Vedran Pavic
9395
*/
9496
@ExtendWith(OutputCaptureExtension.class)
9597
class LiquibaseAutoConfigurationTests {
@@ -373,6 +375,20 @@ void overrideUser() {
373375
}));
374376
}
375377

378+
@Test
379+
@WithDbChangelogMasterYamlResource
380+
void lazyConnectionDataSource() {
381+
String jdbcUrl = "jdbc:h2:mem:liquibase-" + UUID.randomUUID();
382+
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
383+
.withPropertyValues("spring.datasource.url:" + jdbcUrl, "spring.datasource.username:not-sa",
384+
"spring.datasource.connection-fetch:lazy", "spring.liquibase.user:sa")
385+
.run(assertLiquibase((liquibase) -> {
386+
SimpleDriverDataSource dataSource = (SimpleDriverDataSource) liquibase.getDataSource();
387+
assertThat(dataSource.getUrl()).isEqualTo(jdbcUrl);
388+
assertThat(dataSource.getUsername()).isEqualTo("sa");
389+
}));
390+
}
391+
376392
@Test
377393
@WithDbChangelogMasterYamlResource
378394
void overrideUserWhenCustom() {

0 commit comments

Comments
 (0)