From da556a3903f060db77af3b2d083eaacf5b42df79 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 24 Jul 2026 19:28:07 -0500 Subject: [PATCH] Make some corrections to a recent TIL --- python/commit-writes-from-executed-sqlite-statements.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/python/commit-writes-from-executed-sqlite-statements.md b/python/commit-writes-from-executed-sqlite-statements.md index 0511c09..44450d8 100644 --- a/python/commit-writes-from-executed-sqlite-statements.md +++ b/python/commit-writes-from-executed-sqlite-statements.md @@ -35,12 +35,11 @@ the changes and nothing else raised before I committed, then those changes will all be applied atomically. I will need to do my own exception handling with a try/catch that handles any -rollback and closes the connection. +rollback. I'd rather not have to manage those extra pieces which is the kind of thing context managers typically help with. Let's improve upon this with the -[Connection context -manager](https://docs.python.org/3/library/sqlite3.html#how-to-use-the-connection-context-manager): +[Connection context manager](https://docs.python.org/3/library/sqlite3.html#how-to-use-the-connection-context-manager): ```python def write_session_with_project(self, session, *, active=False) -> None: @@ -65,8 +64,8 @@ def write_session_with_project(self, session, *, active=False) -> None: ``` I get the same transactional behavior as before for everything in the context -manager body. However, now the `commit` is handled and the `rollback` and -`close` is handled if there is an exception. +manager body. However, now the `commit` is handled and if an exception occurs +the `rollback` is handled as well. Note: the connection context manager will still re-propagate an exception that occurred, so I may need to handle that with a try/catch somewhere.