Fix NPE with Hikari when DatabaseDriver has null driverClassName

Fixes gh-44994
This commit is contained in:
Andy Wilkinson 2025-04-04 08:38:11 +01:00
parent 88e8c17369
commit cae3a92ead
2 changed files with 13 additions and 1 deletions

View File

@ -190,8 +190,11 @@ public final class DataSourceBuilder<T extends DataSource> {
&& this.values.containsKey(DataSourceProperty.URL)) { && this.values.containsKey(DataSourceProperty.URL)) {
String url = this.values.get(DataSourceProperty.URL); String url = this.values.get(DataSourceProperty.URL);
DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url); DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
String driverClassName = driver.getDriverClassName();
if (driverClassName != null) {
properties.set(dataSource, DataSourceProperty.DRIVER_CLASS_NAME, driver.getDriverClassName()); properties.set(dataSource, DataSourceProperty.DRIVER_CLASS_NAME, driver.getDriverClassName());
} }
}
return dataSource; return dataSource;
} }

View File

@ -474,6 +474,15 @@ class DataSourceBuilderTests {
assertThat(c3p0DataSource.getDriverClass()).isEqualTo("com.example.Driver"); assertThat(c3p0DataSource.getDriverClass()).isEqualTo("com.example.Driver");
} }
@Test
void buildWhenJdbcUrlIsFromUnknownDriverLeavesDriverClassNameUnset() {
this.dataSource = DataSourceBuilder.create()
.url("jdbc:example://localhost:1234/example")
.type(HikariDataSource.class)
.build();
assertThat(((HikariDataSource) this.dataSource).getDriverClassName()).isNull();
}
private DataSource wrap(DataSource target) { private DataSource wrap(DataSource target) {
return new DataSourceWrapper(target); return new DataSourceWrapper(target);
} }