Skip to content

JdbcPreparedStatement ignores query fetch size #4217

@vojta-bisek

Description

@vojta-bisek

Hi,
I'm trying to control the fetch size for a streamed database query response through Spring Data JPA/Hibernate. Anonymized example:

@Repository
public interface ItemRepository extends JpaRepository<Item, Integer> {

  @QueryHints(@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE, value = "123"))
  @Transactional
  Stream<Item> streamAllBy();
}

When tracing the remote H2 2.1.214 database query of 250 items using jdbc:h2:tcp://example.com:1234//path/to/database;TRACE_LEVEL_SYSTEM_OUT=3 shows following JDBC log messages (abridged, if something of importance is missing, I can invest into a more complete anonymized log)

/**/PreparedStatement prep1 = conn0.prepareStatement("...
...
/**/prep1.setFetchSize(123)
...
... repeated processing of each item, in total 100 items ...
RESULT_FETCH_ROWS
... repeated processing of each item, in total 100 items ...
RESULT_FETCH_ROWS
... repeated processing of remaining items.

When stepping through the operation, it appears that the cause is that JdbcPreparedStatement constructor initializes fetchSize in super constructor to 100 (fromSERVER_RESULT_SET_FETCH_SIZE), and immediately prepares command using that initial fetchSize. This fetch size stored in command is subsequently propagated into ResultRemote.fetchAdditionalRows processing that controls the number of items fetched at once.
The invocation of JdbcStatement.setFetchSize that occurs with the expected value 123 only stores the value into JdbcStatement.fetchSize but that is not propagated into the result processing in any way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions