GTT State Persistence Across Db2 LUW Backup Restoration and Hibernate JOIN FETCH Pagination Interaction
0 reputation · 13 Jan 2023, 19:27 UTC
0 reputation · 13 Jan 2023, 19:27 UTC
IBM Db2 LUW global temporary tables (GTTs) exhibit distinct persistence characteristics that may impact application behavior following backup restoration. The research indicates GTTs are session-scoped or transaction-scoped, with session-scoped tables persisting for the connection lifetime but being lost during database restart or connection loss.
During backup restoration, GTT definitions and data are not preserved, requiring recreation of temporary table structures post-restore. This creates a potential inconsistency window where applications expecting GTT availability may fail or exhibit unexpected behavior. The timing of backup restoration relative to active transactions and sessions becomes critical for maintaining application state continuity.
When Hibernate executes queries with JOIN FETCH combined with setFirstResult/setMaxResults, it triggers in-memory pagination rather than database-level LIMIT/OFFSET. This behavior can cause memory exhaustion when processing large datasets, particularly problematic when GTT data needs to be restored and processed in paginated fashion.
The interaction between GTT state loss during backup restoration and Hibernate's in-memory pagination for JOIN FETCH queries creates uncertainty about optimal recovery strategies. Key questions remain about whether applications should implement GTT recreation logic post-backup-restore, how to handle pagination memory constraints during recovery, and whether alternative temporary storage mechanisms would provide better resilience.
34775 reputation · 14 Jan 2023, 00:05 UTC
GTT definitions and data are not preserved during a Db2 LUW backup restoration. Applications must explicitly recreate GTTs and repopulate their data after the restore completes. Hibernate queries that combine JOIN FETCH with setFirstResult/setMaxResults trigger in‑memory pagination, which can exhaust heap when processing large GTT result sets. Avoid this combination or use scrollable results, batch fetching, or native SQL with LIMIT/OFFSET.
ON COMMIT PRESERVE ROWS) exist for the lifetime of a connection. Transaction‑scoped GTTs (ON COMMIT DELETE ROWS) lose data at each commit.SQL0204N (object not found).DECLARE GLOBAL TEMPORARY TABLE statement and re‑insert required rows.JOIN FETCH together with setFirstResult/setMaxResults, Hibernate cannot push the limit/offset to the database because the join would change the row count semantics. It therefore fetches the full result set into memory and applies pagination in the JVM.OutOfMemoryError or severe GC pressure.DECLARE GLOBAL TEMPORARY TABLE SESSION.MY_GTT ... ON COMMIT PRESERVE ROWS (or DELETE ROWS if transaction‑scoped) from a fresh connection after the restore finishes.ON COMMIT DELETE ROWS) or session (for ON COMMIT PRESERVE ROWS) before any Hibernate query touches the table.ScrollableResults with setFetchSize for streaming.JOIN FETCH with a separate batch fetch (@BatchSize or hibernate.default_batch_fetch_size) and paginate the root entity only.LIMIT n OFFSET m (or FETCH FIRST n ROWS ONLY) and map results via ResultTransformer or DTO projection.org.hibernate.SQL=DEBUG) and confirming a single SELECT ... LIMIT ? OFFSET ? (or FETCH FIRST) is issued, not a full select followed by in‑memory slicing.| Pattern | Backup/Restore Resilience | Trade‑offs |
|---|---|---|
| Regular table in a dedicated schema with a cleanup job | Survives backup/restore automatically | Requires manual cleanup; concurrency control via row‑level keys |
Db2 LUW CREATED TEMPORARY TABLE (module‑level) | Definition persists; data does not | Still needs repopulation; less common in Hibernate apps |
| Application‑side in‑memory cache (e.g., Caffeine, Redis) | Independent of database restore | Adds infrastructure; cache invalidation complexity |
For most Hibernate applications, a regular table with a SESSION_ID or REQUEST_ID column and a scheduled purge job provides the simplest restore resilience.
What Hibernate version and Db2 dialect (e.g., org.hibernate.dialect.DB2Dialect vs. DB210Dialect) are you using? Newer dialects may support LIMIT/OFFSET pushdown for certain JOIN FETCH patterns, which would change the pagination mitigation strategy.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.