示例#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
示例#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
示例#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
示例#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
示例#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
示例#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
示例#7
0
文件: base.py 项目: billyhunt/osf.io
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)
示例#8
0
文件: base.py 项目: dplorimer/osf
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)
示例#9
0
文件: context.py 项目: pazthor/osf.io
 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
示例#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
示例#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
示例#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
示例#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()
示例#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()
示例#15
0
文件: handlers.py 项目: ajski/osf.io
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
示例#16
0
文件: context.py 项目: pazthor/osf.io
 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
示例#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
示例#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
示例#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
示例#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
示例#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