Example #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
Example #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
Example #3
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
Example #4
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
Example #5
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
Example #6
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
Example #7
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
Example #8
0
 def commit():
     commands.commit()
     print('Transaction committed.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')
Example #9
0
 def commit():
     commands.commit()
     print('Transaction committed.')
     if auto_transact:
         commands.begin()
         print('New transaction opened.')
Example #10
0
 def tearDown(self):
     try:
         commands.commit()
     except Exception:
         pass
     super(ContextTestCase, self).tearDown()
Example #11
0
 def tearDown(self):
     try:
         commands.commit()
     except Exception:
         pass
     super(ContextTestCase, self).tearDown()