Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL supplier in R2DBC DatabaseClient is eagerly invoked #29367

Closed
EnricSala opened this issue Oct 23, 2022 · 0 comments
Closed

SQL supplier in R2DBC DatabaseClient is eagerly invoked #29367

EnricSala opened this issue Oct 23, 2022 · 0 comments
Assignees
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

Comments

@EnricSala
Copy link
Contributor

Affects spring-r2dbc 5.3.23


The R2DBC DatabaseClient offers two methods to provide the SQL query:

  1. GenericExecuteSpec sql(String sql);
  2. 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 the Publisher is actually subscribed.

However, it seems that the sqlSupplier is being invoked eagerly on assembly. For example this test doesn't pass:

@Test
void sqlSupplierInvocationIsDeferredUntilSubscription() {
  DatabaseClient client = ...;
  Clock clock = ...;

  AtomicBoolean invoked = new AtomicBoolean(false);

  // Assemble a publisher, but don't subscribe yet
  Mono<Void> operation =
      client.sql(
              () -> {
                invoked.set(true);

                return String.format(
                    "SELECT * FROM test WHERE ts < '%s'",
                    clock.instant());
              })
          .map(r -> r.get("ts", Instant.class))
          .all()
          .then();

  // Unexpectedly, the SQL supplier has been invoked
  assertThat(invoked).isFalse();

  // Would expect SQL is deferred until subscription
  operation.block();
  assertThat(invoked).isTrue();
}

The issue appears to happen in DefaultGenericExecuteSpec#execute, which eagerly invokes the sqlSupplier to hold a reference and pass it to several steps of the processing, see:

private <T> FetchSpec<T> execute(Supplier<String> sqlSupplier, BiFunction<Row, RowMetadata, T> mappingFunction) {
String sql = getRequiredSql(sqlSupplier);
Function<Connection, Statement> statementFunction = connection -> {
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL statement [" + sql + "]");

As a workaround, we are currently wrapping the chain in a Mono.defer(() -> ...), but this is inconvenient and easy to forget.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 23, 2022
@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Nov 10, 2022
@sbrannen sbrannen added this to the Triage Queue milestone Nov 10, 2022
@rstoyanchev rstoyanchev removed this from the Triage Queue milestone Jan 20, 2023
@simonbasle simonbasle self-assigned this Jan 24, 2023
@simonbasle simonbasle added type: documentation A documentation task type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 24, 2023
@simonbasle simonbasle added this to the 6.0.x milestone Jan 24, 2023
simonbasle added a commit to simonbasle/spring-framework that referenced this issue Jan 25, 2023
This commit modifies the `DefaultDatabaseClient` implementation in order
to ensure lazier usage of the `Supplier<String>` passed to the sql
method (`DatabaseClient#sql(Supplier)`).

Since technically `DatabaseClient` is an interface that could have 3rd
party implementations, the lazyness expectation is only hinted at in the
`DatabaseClient#sql` javadoc.

Possible caveat: some log statements attempt to reflect the now lazily
resolved SQL string. Similarly, some exceptions can capture the SQL that
caused the issue if known. We expect that these always occur after the
execution of the statement has been attempted (see `ResultFunction`).
At this point the SQL string will be accessible and logs and exceptions
should reflect it as before. Keep an eye out for such strings turning
into `null` after this change, which would indicate the opposite.

Closes spring-projectsgh-29367
@simonbasle simonbasle added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Jan 26, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Jan 26, 2023
simonbasle added a commit that referenced this issue Jan 26, 2023
This commit modifies the `DefaultDatabaseClient` implementation in order
to ensure lazier usage of the `Supplier<String>` passed to the sql
method (`DatabaseClient#sql(Supplier)`).

Since technically `DatabaseClient` is an interface that could have 3rd
party implementations, the lazyness expectation is only hinted at in the
`DatabaseClient#sql` javadoc.

Possible caveat: some log statements attempt to reflect the now lazily
resolved SQL string. Similarly, some exceptions can capture the SQL that
caused the issue if known. We expect that these always occur after the
execution of the statement has been attempted (see `ResultFunction`).
At this point the SQL string will be accessible and logs and exceptions
should reflect it as before. Keep an eye out for such strings turning
into `null` after this change, which would indicate the opposite.

Backport of d72df5a
See gh-29367
Closes gh-29887
@simonbasle simonbasle modified the milestones: 6.0.x, 6.0.5 Jan 26, 2023
@jhoeller jhoeller removed the type: documentation A documentation task label Feb 1, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
This commit modifies the `DefaultDatabaseClient` implementation in order
to ensure lazier usage of the `Supplier<String>` passed to the sql
method (`DatabaseClient#sql(Supplier)`).

Since technically `DatabaseClient` is an interface that could have 3rd
party implementations, the lazyness expectation is only hinted at in the
`DatabaseClient#sql` javadoc.

Possible caveat: some log statements attempt to reflect the now lazily
resolved SQL string. Similarly, some exceptions can capture the SQL that
caused the issue if known. We expect that these always occur after the
execution of the statement has been attempted (see `ResultFunction`).
At this point the SQL string will be accessible and logs and exceptions
should reflect it as before. Keep an eye out for such strings turning
into `null` after this change, which would indicate the opposite.

Closes spring-projectsgh-29367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

6 participants