Skip to content

DataSourceBuilder throws an UnsupportedDataSourcePropertyException when spring is loaded with a different classloader than dataSourceType #49238

@ftreede

Description

@ftreede

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions