Several Changes, One Outcome
The Connection Is Still Open
with connection: looks similar to opening a file with with, but the two contexts promise different things. A SQLite connection context finishes a
Here is a complete caller after the successful pair from the previous exercise:
result = checkout_pair(
connection,
(7301, 4, 17, "2026-08-26"),
(7302, 5, 28, "2026-08-26"),
)
count = connection.execute("SELECT COUNT(*) FROM loans").fetchone()[0]
The observation is:
checkout_pair(...) -> True
Same connection loan count: 5
Fresh connection new loan IDs: [7301, 7302]
Connection still open: yes
The fresh connection proves durability: the context committed the two new rows. The count through the original connection proves lifetime: the checkout_pair remains usable after the context ended.
This division of responsibility keeps checkout_pair decides the outcome of its changes, but its caller may want to run another query, return a loan, or perform another operation with the same connection. The caller that opened the connection eventually calls close().
The database file has a third lifetime. Closing one connection does not delete a file-backed database. A later connection can open the same path and read committed rows. In these exercises, the displayed database lives only inside one temporary Run, so nothing is promised after that Run ends or after Reset project.
What does leaving with connection: do after both inserts succeed?
With the lifetime clear, you can use the same transaction pattern to return one active loan without taking ownership of the connection.