Skip to content

Commit

Permalink
Close the connection when using a context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
wlwlwlzhang committed Sep 5, 2023
1 parent 5c2dc8d commit b6ff3e5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions dataset/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,22 @@ def __enter__(self):
return self

def __exit__(self, error_type, error_value, traceback):
"""End a transaction by committing or rolling back."""
if error_type is None:
try:
self.commit()
except Exception:
with safe_reraise():
self.rollback()
else:
self.rollback()
"""End a transaction by committing or rolling back. Close local connection"""
try:
if error_type is None:
try:
self.commit()
except Exception:
with safe_reraise():
self.rollback()
else:
self.rollback()
except Exception:
raise
finally:
tx_conn = self.connections.pop(threading.get_ident(), None)
if tx_conn is not None:
tx_conn.close()

def close(self):
"""Close database connections. Makes this object unusable."""
Expand Down

0 comments on commit b6ff3e5

Please sign in to comment.