Skip to content

Commit

Permalink
database: handle failure on transation commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Nyhus committed Feb 16, 2024
1 parent 5a79150 commit 05c439c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ def __init__(self, cursor, name):
self._name = name

def __enter__(self):
log.debug('Transaction start, %s', self._name)
self._cursor.execute('begin')
return self._cursor

def __exit__(self, type, value, traceback):
if type is None and value is None and traceback is None:
self._cursor.execute('commit')
log.debug('Transaction commit, %s', self._name)
try:
self._cursor.execute('commit')
except sqlite3.OperationalError as e:
self._cursor.execute('rollback')
raise
else:
self._cursor.execute('rollback')
log.debug('Transaction rollback, %s', self._name)

return Transaction(self._cursor(), name)

Expand Down

0 comments on commit 05c439c

Please sign in to comment.