-
Notifications
You must be signed in to change notification settings - Fork 41.9k
Description
We have a setup where the spring boot classes are loaded by a different classloader than our application classes. The application class loader is set as bean class loader so things generally work.
In this, we now added a JDBC data source that is to be constructed via DataSourceBuilder. We want to use Hikari so we hardcoded the type in our configuration class:
@Bean
@ExtraDB
@ConfigurationProperties("extradb.datasource.hikari")
DataSource extradbDataSource(@ExtraDB DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
However, we got an exception:
org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'extradbDataSource' threw exception with message: Unable to find suitable method for url
Investigating this, the reason seems to come from the way org.springframework.boot.jdbc.DataSourceBuilder.MappedDataSourceProperties#forType works. It uses the passed bean class loader to check for the presence of the data source class, but then loads that class using it's own class loader.
In our case, the two classloaders ended up each loading their own version of HikariDataSource, which caused a mismatch and the subsequent fallback to ReflectionDataSourceProperties which cannot deal with Hikari.