Brief
Decide when to confirm a batch and record its checkpoint in agreement with rows actually stored.
In the story, a vendor service stopped just after writing B1. Imani has E7 and two forms of TRANSFER-7. Mara gives you a simulation: B1 has E7 and TRANSFER-7, B2 has E9, and confirmed count starts at 0. The practice database can hold rows and checkpoint in one transaction. Voss claims “processed” means “shown once on screen”; explain a more precise rule.
Your mission
- Give the confirmed count after B1 and after B2 once each is truly checked.
- Describe the correct order of actions inside B1’s transaction.
- Give expected state if the program stops before or after B1’s
COMMIT. - Explain what can happen if the database keeps B1 while an external notebook still says 0.
How to check your work
- After B1, this exercise’s offset is 2; after B2, 3.
- Insert, check both rows, update checkpoint and
COMMITbelong to one group. - Before commit: 0 rows and 0 confirmed; afterward: 2 rows and 2 confirmed.
- A notebook at 0 may replay B1; existing events must be recognized before republishing.
Course example
import sqlite3
db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE events (event_id TEXT)')
db.execute('CREATE TABLE checkpoints (name TEXT PRIMARY KEY, confirmed_count INTEGER NOT NULL)')
db.execute("INSERT INTO checkpoints VALUES ('vendor', 0)")
db.commit()
batch = ['E7', 'TRANSFER-7']
try:
db.execute('BEGIN')
db.executemany('INSERT INTO events VALUES (?)', [(event_id,) for event_id in batch])
stored = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
if stored != len(batch):
raise ValueError('batch count mismatch')
db.execute("UPDATE checkpoints SET confirmed_count = ? WHERE name = 'vendor'", (2,))
db.commit()
except Exception:
db.rollback()
raise
print(db.execute('SELECT COUNT(*) FROM events').fetchone()[0])
print(db.execute("SELECT confirmed_count FROM checkpoints WHERE name = 'vendor'").fetchone()[0])
db.close()