Several Changes, One Outcome · practice
Roll Back the Whole Attempt
The missing-member call now has a named failure: sqlite3.IntegrityError. Its earlier insert is still pending, so the
Open transactions.py. Wrap the two inserts, commit, and successful return in one try block. Then add this narrow handler:
except sqlite3.IntegrityError:
connection.rollback()
return False
rollback() discards changes that are still pending on this connection. It does not delete old committed loans. Here, it removes loan 7101, the valid prefix of the failed pair.
Keep connection.commit() after both inserts in the successful branch. The complete behavior is now:
| Outcome | ||
|---|---|---|
| Both inserts succeed | Commit both | True |
| Either insert breaks an integrity rule | Roll back pending work | False |
Press Run. The valid block should still reopen with IDs [7001, 7002]. The missing-member block should now report False, no escaping database-rule error, and empty proposed IDs on both connections.
The handler catches only sqlite3.IntegrityError. Do not catch Exception or all sqlite3.Error values. If a different defect appears, such as invalid SQL or an unusable Python False would make it look like the known catalog outcome. That unrelated failure should remain visible.
Submit places the bad row first in one case and second in another. It also starts with unrelated committed rows and verifies they remain. A rollback should resolve this attempted pair, not clear the table. Finally, it runs a harmless query through the supplied connection, because resolving the transaction does not transfer ownership of the connection to this function.
This manual branch is correct and worth seeing once. You can keep the same results while letting the connection manage its normal commit and rollback decisions.
Task
Update checkout_pair so a valid pair commits and returns True. Catch only sqlite3.IntegrityError; roll back the pending attempt and return False.
Preserve existing committed rows, let unrelated errors escape, and leave the supplied connection open.