SQL supplier in R2DBC DatabaseClient
is eagerly invoked
#29367
Labels
in: data
Issues in data modules (jdbc, orm, oxm, tx)
status: backported
An issue that has been backported to maintenance branches
type: enhancement
A general enhancement
Milestone
Affects
spring-r2dbc
5.3.23The R2DBC
DatabaseClient
offers two methods to provide the SQL query:GenericExecuteSpec sql(String sql);
GenericExecuteSpec sql(Supplier<String> sqlSupplier);
We were expecting that the latter would evaluate the
sqlSupplier
lazily, i.e. deferred until subscription. For example we were attempting to use this supplier for large or dynamic queries which we would like to build only in cases where thePublisher
is actually subscribed.However, it seems that the
sqlSupplier
is being invoked eagerly on assembly. For example this test doesn't pass:The issue appears to happen in
DefaultGenericExecuteSpec#execute
, which eagerly invokes thesqlSupplier
to hold a reference and pass it to several steps of the processing, see:spring-framework/spring-r2dbc/src/main/java/org/springframework/r2dbc/core/DefaultDatabaseClient.java
Lines 325 to 329 in 5ea8ba1
As a workaround, we are currently wrapping the chain in a
Mono.defer(() -> ...)
, but this is inconvenient and easy to forget.The text was updated successfully, but these errors were encountered: