Improved recovery when the connection is closed in an atomic block.

This commit is contained in:
Aymeric Augustin 2013-03-13 14:54:44 +01:00
parent 83a416f5e7
commit 93af822c00
1 changed files with 8 additions and 2 deletions

View File

@ -52,14 +52,14 @@ class BaseDatabaseWrapper(object):
self._dirty = False self._dirty = False
# Tracks if the connection is in a transaction managed by 'atomic'. # Tracks if the connection is in a transaction managed by 'atomic'.
self.in_atomic_block = False self.in_atomic_block = False
# List of savepoints created by 'atomic'
self.savepoint_ids = []
# Tracks if the outermost 'atomic' block should commit on exit, # Tracks if the outermost 'atomic' block should commit on exit,
# ie. if autocommit was active on entry. # ie. if autocommit was active on entry.
self.commit_on_exit = True self.commit_on_exit = True
# Tracks if the transaction should be rolled back to the next # Tracks if the transaction should be rolled back to the next
# available savepoint because of an exception in an inner block. # available savepoint because of an exception in an inner block.
self.needs_rollback = False self.needs_rollback = False
# List of savepoints created by 'atomic'
self.savepoint_ids = []
# Connection termination related attributes # Connection termination related attributes
self.close_at = None self.close_at = None
@ -100,6 +100,9 @@ class BaseDatabaseWrapper(object):
def connect(self): def connect(self):
"""Connects to the database. Assumes that the connection is closed.""" """Connects to the database. Assumes that the connection is closed."""
# In case the previous connection was closed while in an atomic block
self.in_atomic_block = False
self.savepoint_ids = []
# Reset parameters defining when to close the connection # Reset parameters defining when to close the connection
max_age = self.settings_dict['CONN_MAX_AGE'] max_age = self.settings_dict['CONN_MAX_AGE']
self.close_at = None if max_age is None else time.time() + max_age self.close_at = None if max_age is None else time.time() + max_age
@ -179,6 +182,9 @@ class BaseDatabaseWrapper(object):
Closes the connection to the database. Closes the connection to the database.
""" """
self.validate_thread_sharing() self.validate_thread_sharing()
# Don't call validate_no_atomic_block() to avoid making it difficult
# to get rid of a connection in an invalid state. The next connect()
# will reset the transaction state anyway.
try: try:
self._close() self._close()
finally: finally: