r/learnprogramming • u/Think_Percentage_959 • 3d ago
Need help on DB2 design
Problem Statement
We have a transaction history table (CARDTXNHIST) used by a banking application to display customer transactions.
The daily batch process consists of two stages:
Unstatemented Transactions (Code = 2) batch file → TEMP table → View → CARDTXNHIST
Existing Code=2 records are deleted and refreshed
Statemented Transactions (Code = 3)
Separate batch file → TEMP table → View → CARDTXNHIST
Statemented records are loaded after unstatemented processing
The batch window runs approximately between 4 AM and 9 AM.
Observed Issue
On statement cycle dates, the incoming unstatemented dataset may be empty because all eligible transactions have been statemented.
As a result:
Code=2 transactions are removed/refreshed
Code=3 transactions are still being processed
During this period, customers may see no transaction history in the application, even though the transactions still exist within the batch pipeline.
Objective
We are looking for database/application architecture approaches that can eliminate or minimize this visibility gap while the batch is in progress.
Solutions Under Consideration
Retaining Code=2 records until Code=3 processing completes
Shadow table / table-swap approach
Composite views
Deferred commit strategy
Reading from staging/view sources during batch processing
Data archival and performance optimization
Other database design recommendations
Questions
What would be the recommended design pattern to ensure customers always see a consistent transaction history?
How have similar large-scale batch processing systems solved this "delete-before-republish" visibility gap?
Are there DB2-specific approaches (isolation levels, shadow tables, transition tables, view-switching, etc.) that would be suitable for this scenario?
If you'd like to adjust the length or add any extra context (like table volume or specific DB2 version) before posting, let me know!
1
u/forestking101 3d ago
Staging tables exist so live readers do not watch sausage getting made.
1
u/NW1969 3d ago
This feels like a process issue, not a technology issue:
- process code 3 transactions before code 2 transactions
- include code 2 transactions in the batch file when you know there is a corresponding code 3 transaction (and remove the code 2 when you process the code 3)
- probably lots of other process-based solutions
1
u/Lucky_Cable_3145 3d ago
Maybe I'm not understanding the scenario...
The statement update requires both files to succeed.
I would want to validate both files before any processing. [ie what if both files are empty or one is corrupted?]
I would load both to a staging table (including type 2/3) then process all at once or in logically windowed transactions (if performance constrained).
1
u/navicat-gu 1d ago
The gap exists because readers can see the moment between "Code=2 rows deleted" and "Code=3 rows committed". So the fix is to make the change from old to new data a single step, from the reader's point of view.
Options, from least to most change:
1. One unit of work. Load both files into staging tables first and validate them (counts, an empty Code=2 file being expected on statement dates, etc.). Then do the delete of the old Code=2 rows and the insert of the new Code=2 and Code=3 rows in one transaction with a single
COMMITat the end. On DB2 LUW with currently committed semantics (the default for new databases since 9.7), readers at Cursor Stability keep seeing the old committed rows instead of an empty result or a lock wait. For big volumes, commit in batches only on a staging/shadow table, never on the live one.2. Shadow table + switch. Keep two copies (
CARDTXNHIST_A/_B), point the app at an alias or view, load the inactive copy completely, then repoint the alias/view in one short transaction. The app never sees a partial load, and you have an instant rollback (point back to the old copy).3. Don't delete, supersede. Never delete Code=2 rows during the load. Mark them with a batch id or effective date, and let the view show the latest complete batch only (
WHERE batch_id = (SELECT MAX(batch_id) FROM batch_control WHERE status = 'COMPLETE')). Clean up old batches afterwards.Whichever you pick, add a
batch_controltable with status per stage. It also gives you a clean way to handle "one file failed" without leaving customers with partial history.I'd go with 1 if the volumes fit in the window, 2 if they don't.
Disclosure: I'm RC, Director of Digital Strategy @ Navicat. Navicat 18 doesn't connect to DB2, so I won't plug it here; the design above doesn't depend on any tool anyway. If you also work with MySQL, PostgreSQL, SQL Server or Oracle, Navicat Premium Lite is free to try.