Skip to content

spring.datasource.hikari.data-source-class-name cannot be used as a driver class name is always required and Hikari does not accept both #44938

Closed
@michael-simons

Description

@michael-simons

Given a valid javax.sql.DataSource implementation I expect to be able to used it as type in

spring.datasource.type

to configure my application.

However, this fails:

registry.add("spring.datasource.type", MyDataSource.class::getCanonicalName);
registry.add("spring.datasource.driver-class-name", MyDriver.class::getCanonicalName);
registry.add("spring.datasource.url", () ->
	"jdbc:vendor://localhost"
);
registry.add("spring.datasource.username", () -> "foo");
registry.add("spring.datasource.password", () -> "bar");

with

Caused by: org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException: Unable to find suitable method for url
	at app//org.springframework.boot.jdbc.UnsupportedDataSourcePropertyException.throwIf(UnsupportedDataSourcePropertyException.java:36)
	at app//org.springframework.boot.jdbc.DataSourceBuilder$ReflectionDataSourceProperties.getMethod(DataSourceBuilder.java:581)
	at app//org.springframework.boot.jdbc.DataSourceBuilder$ReflectionDataSourceProperties.set(DataSourceBuilder.java:563)
	at app//org.springframework.boot.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:184)
	at app//org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:59)

I can of course not omit the URL, as this is required for the connection.

However, setUrl is not defined part of the DataSource interface, yet the builder insists to propagate all non-null properties from the datasource properties in DataSourceBuilder#build.

It will not fail on Hikari because there's dedicated config class.

Reproducer is attached.

@SpringBootTest
class DemoApplicationTests {

	@DynamicPropertySource
	static void postgresqlProperties(DynamicPropertyRegistry registry) {
		registry.add("spring.datasource.type", MyDataSource.class::getCanonicalName);
		registry.add("spring.datasource.driver-class-name", MyDriver.class::getCanonicalName);
		registry.add("spring.datasource.url", () ->
			"jdbc:vendor://localhost"
		);
		registry.add("spring.datasource.username", () -> "foo");
		registry.add("spring.datasource.password", () -> "bar");
	}

	@Test
	void contextLoads() {
	}

}

demo.zip

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions