mirror of
https://github.com/jbranchaud/til
synced 2026-08-31 16:51:46 +00:00
Make some corrections to a recent TIL
This commit is contained in:
@@ -35,12 +35,11 @@ the changes and nothing else raised before I committed, then those changes will
|
|||||||
all be applied atomically.
|
all be applied atomically.
|
||||||
|
|
||||||
I will need to do my own exception handling with a try/catch that handles any
|
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
|
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
|
context managers typically help with. Let's improve upon this with the
|
||||||
[Connection context
|
[Connection context manager](https://docs.python.org/3/library/sqlite3.html#how-to-use-the-connection-context-manager):
|
||||||
manager](https://docs.python.org/3/library/sqlite3.html#how-to-use-the-connection-context-manager):
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def write_session_with_project(self, session, *, active=False) -> None:
|
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
|
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
|
manager body. However, now the `commit` is handled and if an exception occurs
|
||||||
`close` is handled if there is an exception.
|
the `rollback` is handled as well.
|
||||||
|
|
||||||
Note: the connection context manager will still re-propagate an exception that
|
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.
|
occurred, so I may need to handle that with a try/catch somewhere.
|
||||||
|
|||||||
Reference in New Issue
Block a user