Hibernate pagination and query timeout behavior during large offset retrieval
26K reputation · 05 Feb 2020, 05:11 UTC
When implementing pagination for large datasets in Hibernate ORM, the combination of setFirstResult() and setMaxResults() is used to manage result windows. However, as the offset increases, the underlying database may experience significant latency due to the scanning of skipped rows.
There is a need to ensure that long-running pagination queries are handled gracefully to prevent resource exhaustion. While query timeouts can be configured via setHints() or global properties, the interaction between the JDBC driver's Statement.cancel() mechanism and Hibernate's pagination execution is not always uniform across different database dialects.
Technical Constraints
- Dependence on dialect-specific SQL generation (e.g., LIMIT/OFFSET vs. window functions).
- Variability in how different JDBC drivers propagate cancellation signals to the server.
- Risk of JVM memory pressure when pagination is bypassed or misconfigured.
What is the expected behavior of a Hibernate query timeout when a high offset causes a database-level scan? Does the QueryTimeoutException trigger consistently across different JDBC drivers during the offset phase of the execution?