Skip to content

Commit

Permalink
Raise OperationalError when close() is called while txn active.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Feb 22, 2018
1 parent 207a4c5 commit f40ec03
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,9 @@ def close(self):
if self.deferred:
raise Exception('Error, database must be initialized before '
'opening a connection.')
if self.in_transaction():
raise OperationalError('Attempting to close database while '
'transaction is open.')
is_open = not self._state.closed
try:
if is_open:
Expand Down
4 changes: 4 additions & 0 deletions tests/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,7 @@ def also_fails():

with db.manual_commit():
self.assertRaises(ValueError, also_fails)

def test_closing_db_in_transaction(self):
with db.atomic():
self.assertRaises(OperationalError, db.close)

0 comments on commit f40ec03

Please sign in to comment.