spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Allow Data JDBC Dialect resolution without requiring DataSource initialization

Open mp911de opened this issue 2 months ago • 5 comments

When obtaining a Dialect bean using Spring Data JDBC, the @Bean method depends on NamedParameterJdbcOperations leading to initialization of the entire bean graph including DataSource initialization even if the dialect is provided through properties (spring.data.jdbc.dialect=…). It would be good if access to NamedParameterJdbcOperations could be lazy.

The JDBC config itself isn't helpful in that regard as it declares NamedParameterJdbcOperations in its signature. Maybe an opportunity for improvement as well.

mp911de avatar Oct 23 '25 14:10 mp911de

The JDBC config itself isn't helpful in that regard as it declares NamedParameterJdbcOperations in its signature.

Indeed, I think our hands are tied here without a change to the signature of AbstractJdbcConfiguration.jdbcDialect(NamedParameterJdbcOperations) that we override.

wilkinsona avatar Oct 23 '25 14:10 wilkinsona

We could introduce a @Bean Dialect jdbcDialect() method on the config class and forward the call to Dialect jdbcDialect(NamedParameterJdbcOperations operations) but honestly, the AbstractJdbcConfiguration class would require a larger revision and changing bean methods is a breaking change on its own. Maybe worth targeting 4.1 with a proper deprecation cycle.

mp911de avatar Oct 23 '25 15:10 mp911de

Alright, I've put this in 4.x and labelled it as blocked. If you open an issue for the changes on the Spring Data JDBC side, please let us know so that we can track its progress.

wilkinsona avatar Oct 23 '25 15:10 wilkinsona

The workaround is to explicitly define dialect bean - otherwise processAot phase in Gradle or Maven fails:

/**
 * hints for AOT processing - otherwise Spring Boot will try to connect to the DB
 */
@Bean
@ConditionalOnMissingBean
fun jdbcDialect(): JdbcDialect = JdbcPostgresDialect.INSTANCE

bedrin avatar Nov 19 '25 08:11 bedrin

We've introduced with https://github.com/spring-projects/spring-data-relational/issues/2165 a utility for better reuse of configuration functionality. Because of such a late change in Spring Data JDBC, it wasn't possible to integrate the change in Spring Boot. We're happy to refine JdbcConfiguration for better composition if the Boot config is missing functionality to lower requirements for certain configuration arrangements and delaying database access.

mp911de avatar Nov 19 '25 09:11 mp911de