Skip to content

Commit cae3a92

Browse files
committed
Fix NPE with Hikari when DatabaseDriver has null driverClassName
Fixes gh-44994
1 parent 88e8c17 commit cae3a92

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ public T build() {
190190
&& this.values.containsKey(DataSourceProperty.URL)) {
191191
String url = this.values.get(DataSourceProperty.URL);
192192
DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
193-
properties.set(dataSource, DataSourceProperty.DRIVER_CLASS_NAME, driver.getDriverClassName());
193+
String driverClassName = driver.getDriverClassName();
194+
if (driverClassName != null) {
195+
properties.set(dataSource, DataSourceProperty.DRIVER_CLASS_NAME, driver.getDriverClassName());
196+
}
194197
}
195198
return dataSource;
196199
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,15 @@ void buildWhenC3P0TypeSpecifiedReturnsExpectedDataSource() {
474474
assertThat(c3p0DataSource.getDriverClass()).isEqualTo("com.example.Driver");
475475
}
476476

477+
@Test
478+
void buildWhenJdbcUrlIsFromUnknownDriverLeavesDriverClassNameUnset() {
479+
this.dataSource = DataSourceBuilder.create()
480+
.url("jdbc:example://localhost:1234/example")
481+
.type(HikariDataSource.class)
482+
.build();
483+
assertThat(((HikariDataSource) this.dataSource).getDriverClassName()).isNull();
484+
}
485+
477486
private DataSource wrap(DataSource target) {
478487
return new DataSourceWrapper(target);
479488
}

0 commit comments

Comments
 (0)