Avoid triggering NPE in Hikari with null driverClassName
Previously, the post-processing of HikariDataSource to apply custom connection details would trigger an NPE in Hikari if those details supplied a null driverClassName. This commit avoids the problem by only setting the driverClassName when it is non-null. Closes gh-44997
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,7 +39,10 @@ class HikariJdbcConnectionDetailsBeanPostProcessor extends JdbcConnectionDetails
|
||||
dataSource.setJdbcUrl(connectionDetails.getJdbcUrl());
|
||||
dataSource.setUsername(connectionDetails.getUsername());
|
||||
dataSource.setPassword(connectionDetails.getPassword());
|
||||
dataSource.setDriverClassName(connectionDetails.getDriverClassName());
|
||||
String driverClassName = connectionDetails.getDriverClassName();
|
||||
if (driverClassName != null) {
|
||||
dataSource.setDriverClassName(connectionDetails.getDriverClassName());
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.jdbc.DatabaseDriver;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link HikariJdbcConnectionDetailsBeanPostProcessor}.
|
||||
@@ -47,4 +48,13 @@ class HikariJdbcConnectionDetailsBeanPostProcessorTests {
|
||||
assertThat(dataSource.getDriverClassName()).isEqualTo(DatabaseDriver.POSTGRESQL.getDriverClassName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toleratesConnectionDetailsWithNullDriverClassName() {
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
dataSource.setDriverClassName(DatabaseDriver.H2.getDriverClassName());
|
||||
JdbcConnectionDetails connectionDetails = mock(JdbcConnectionDetails.class);
|
||||
new HikariJdbcConnectionDetailsBeanPostProcessor(null).processDataSource(dataSource, connectionDetails);
|
||||
assertThat(dataSource.getDriverClassName()).isEqualTo(DatabaseDriver.H2.getDriverClassName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user