Skip to content

Commit 4611230

Browse files
Ronald Mikwilkinsona
authored andcommitted
Derive driverClassName from URL when otherwise unknown
See gh-39376
1 parent 82706ac commit 4611230

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public T build() {
186186
}
187187
if (!applied.contains(DataSourceProperty.DRIVER_CLASS_NAME)
188188
&& properties.canSet(DataSourceProperty.DRIVER_CLASS_NAME)
189-
&& this.values.containsKey(DataSourceProperty.URL)) {
190-
String url = this.values.get(DataSourceProperty.URL);
189+
&& applied.contains(DataSourceProperty.URL)) {
190+
String url = properties.get(dataSource, DataSourceProperty.URL);
191191
DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
192192
properties.set(dataSource, DataSourceProperty.DRIVER_CLASS_NAME, driver.getDriverClassName());
193193
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,20 @@ void buildWhenDerivedFromCustomTypeWithTypeChange() {
457457
assertThat(testSource.getPassword()).isEqualTo("secret");
458458
}
459459

460+
@Test
461+
void buildWhenDerivedFromCustomTypeDeriveDriverClassNameFromDerivedUrl() {
462+
UrlCapableLimitedCustomDataSource dataSource = new UrlCapableLimitedCustomDataSource();
463+
dataSource.setUsername("test");
464+
dataSource.setPassword("secret");
465+
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
466+
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class);
467+
SimpleDriverDataSource testSource = (SimpleDriverDataSource) builder.build();
468+
assertThat(testSource.getUsername()).isEqualTo("test");
469+
assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
470+
assertThat(testSource.getPassword()).isEqualTo("secret");
471+
assertThat(testSource.getDriver()).isInstanceOf(org.postgresql.Driver.class);
472+
}
473+
460474
@Test // gh-31920
461475
void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource() {
462476
this.dataSource = DataSourceBuilder.create()
@@ -620,12 +634,10 @@ void setPassword(String password) {
620634

621635
}
622636

623-
static class CustomDataSource extends LimitedCustomDataSource {
637+
static class UrlCapableLimitedCustomDataSource extends LimitedCustomDataSource {
624638

625639
private String url;
626640

627-
private String driverClassName;
628-
629641
String getUrl() {
630642
return this.url;
631643
}
@@ -634,6 +646,13 @@ void setUrl(String url) {
634646
this.url = url;
635647
}
636648

649+
}
650+
651+
static class CustomDataSource extends UrlCapableLimitedCustomDataSource {
652+
653+
private String driverClassName;
654+
655+
637656
String getDriverClassName() {
638657
return this.driverClassName;
639658
}

0 commit comments

Comments
 (0)