def process_response(self, request, response): """Commit transaction if it exists, rolling back in an exception occurs. """ try: if response.status_code >= 400: commands.rollback() else: commands.commit() except OperationFailure as err: message = utils.get_error_message(err) if messages.NO_TRANSACTION_TO_COMMIT_ERROR not in message: if settings.DEBUG_TRANSACTIONS: pass else: raise err except Exception as err: try: commands.rollback() except OperationFailure: pass else: raise err commands.disconnect() return response
def clear_transactions(self): try: commands.rollback() except OperationFailure as error: message = utils.get_error_message(error) if messages.NO_TRANSACTION_ERROR not in message: raise
def process_request(self, request): """Begin a transaction if one doesn't already exist.""" try: commands.begin() except OperationFailure as err: message = utils.get_error_message(err) if messages.TRANSACTION_EXISTS_ERROR not in message: raise err
def teardown_database(client=None, database=None): client = client or client_proxy database = database or database_proxy try: commands.rollback(database) except OperationFailure as error: message = utils.get_error_message(error) if messages.NO_TRANSACTION_ERROR not in message: raise client.drop_database(database)
def __enter__(self): try: commands.begin(self.database) self.pending = True except OperationFailure as error: message = utils.get_error_message(error) if messages.TRANSACTION_EXISTS_ERROR not in message: raise logger.warn('Transaction already in progress') return self
def process_exception(self, request, exception): """If an exception occurs, rollback the current transaction if it exists. """ try: commands.rollback() except OperationFailure as err: message = utils.get_error_message(err) if messages.NO_TRANSACTION_ERROR not in message: raise commands.disconnect() return None
def process_exception(self, request, exception): """If an exception occurs, rollback the current transaction if it exists. """ sentry_exception_handler(request=request) try: commands.rollback() except OperationFailure as err: message = utils.get_error_message(err) if messages.NO_TRANSACTION_ERROR not in message: raise commands.disconnect() return None
def transaction_before_request(): """Setup transaction before handling the request. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return None try: commands.rollback() logger.error('Transaction already in progress; rolling back.') except OperationFailure as error: message = utils.get_error_message(error) if messages.NO_TRANSACTION_ERROR not in message: raise commands.begin()
def transaction_teardown_request(error=None): """Rollback transaction on uncaught error. This code should never be reached in debug mode, since uncaught errors are raised for use in the Werkzeug debugger. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return if error is not None: try: commands.rollback() except OperationFailure as error: message = utils.get_error_message(error) if messages.NO_TRANSACTION_ERROR not in message: raise
def __exit__(self, exc_type, exc_val, exc_tb): if self.pending: if exc_type: commands.rollback(self.database) self.pending = False raise exc_val try: commands.commit(self.database) self.pending = False except OperationFailure as error: message = utils.get_error_message(error) if messages.LOCK_ERROR in message: commands.rollback(self.database) self.pending = False raise
def transaction_teardown_request(error=None): """Rollback transaction on uncaught error. This code should never be reached in debug mode, since uncaught errors are raised for use in the Werkzeug debugger. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return if error is not None: try: commands.rollback() except OperationFailure as error: # expected error, transaction should have closed in after_request message = utils.get_error_message(error) if messages.NO_TRANSACTION_ERROR not in message: # unexpected error, not a transaction error, reraise raise
def transaction_after_request(response, base_status_code_error=500): """Teardown transaction after handling the request. Rollback if an uncaught exception occurred, else commit. If the commit fails due to a lock error, rollback and return error response. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return response if response.status_code >= base_status_code_error: commands.rollback() else: try: commands.commit() except OperationFailure as error: message = utils.get_error_message(error) if messages.LOCK_ERROR in message: commands.rollback() return utils.handle_error(LOCK_ERROR_CODE) raise return response
def transaction_after_request(response): """Teardown transaction after handling the request. Rollback if an uncaught exception occurred, else commit. If the commit fails due to a lock error, rollback and return error response. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return response if response.status_code >= 500: commands.rollback() else: try: commands.commit() except OperationFailure as error: message = utils.get_error_message(error) if 'lock not granted' in message.lower(): commands.rollback() return utils.handle_error(LOCK_ERROR_CODE) raise return response
def transaction_after_request(response, base_status_code_error=500): """Teardown transaction after handling the request. Rollback if an uncaught exception occurred, else commit. If the commit fails due to a lock error, rollback and return error response. """ if view_has_annotation(NO_AUTO_TRANSACTION_ATTR): return response if response.status_code >= base_status_code_error: try: commands.rollback() except OperationFailure: logger.exception('Transaction rollback failed after request') else: try: commands.commit() except OperationFailure as error: # transaction commit failed, log and reraise message = utils.get_error_message(error) if messages.LOCK_ERROR in message: commands.rollback() return utils.handle_error(LOCK_ERROR_CODE) raise return response