Exemplo n.º 1
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
Exemplo n.º 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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 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
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
 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
Exemplo n.º 10
0
 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
Exemplo n.º 11
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
Exemplo n.º 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
Exemplo n.º 13
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()
Exemplo n.º 14
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()
Exemplo n.º 15
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
Exemplo n.º 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
Exemplo n.º 17
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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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