Exemple #1
0
 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
Exemple #2
0
    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
Exemple #3
0
    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
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
 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
Exemple #8
0
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 None
    if error is not None:
        if not settings.DEBUG_MODE:
            logger.error('Uncaught error in `transaction_teardown_request`; '
                         'this should never happen with `DEBUG_MODE = True`')
        commands.rollback()
Exemple #9
0
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 None
    if error is not None:
        if not settings.DEBUG_MODE:
            logger.error('Uncaught error in `transaction_teardown_request`; '
                         'this should never happen with `DEBUG_MODE = True`')
        commands.rollback()
Exemple #10
0
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()
Exemple #11
0
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()
Exemple #12
0
 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
Exemple #13
0
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
Exemple #14
0
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 None
    if error is not None:
        if not settings.DEBUG_MODE:
            logger.error('Uncaught error in `transaction_teardown_request`; '
                         'this should never happen with `DEBUG_MODE = True`')
        # If we're testing, the before_request handlers may not have been executed
        # e.g. when Flask#test_request_context() is used
        if not current_app.testing:
            commands.rollback()
Exemple #15
0
 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
Exemple #16
0
 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
Exemple #17
0
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 None
    if error is not None:
        if not settings.DEBUG_MODE:
            logger.error('Uncaught error in `transaction_teardown_request`; '
                         'this should never happen with `DEBUG_MODE = True`')
        # If we're testing, the before_request handlers may not have been executed
        # e.g. when Flask#test_request_context() is used
        if not current_app.testing:
            commands.rollback()
Exemple #18
0
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
Exemple #19
0
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
Exemple #20
0
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
Exemple #21
0
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
Exemple #22
0
 def rollback():
     commands.rollback()
     print('Transaction rolled back.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')
Exemple #23
0
 def rollback():
     commands.rollback()
     print('Transaction rolled back.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')