def _safe_commit(): """Try to commit changes in the session. Roll back if exception raised Excepts SQLAlchemy errors and rollbacks if they're caught """ try: db.session.commit() except sql_errors as e: db.session.rollback() raise manager_exceptions.SQLStorageException( 'SQL Storage error: {0}'.format(str(e)))
def _safe_commit(self): """Try to commit changes in the session. Roll back if exception raised Excepts SQLAlchemy errors and rollbacks if they're caught """ try: db.session.commit() except sql_errors as e: exception_to_raise = manager_exceptions.SQLStorageException( 'SQL Storage error: {0}'.format(str(e))) db.session.rollback() if SQLStorageManager._is_unique_constraint_violation(e): problematic_instance_id = e.params['id'] # Session has been rolled back at this point self.refresh(self.current_tenant) exception_to_raise = manager_exceptions.ConflictError( 'Instance with ID {0} cannot be added on {1} or with ' 'global visibility' ''.format(problematic_instance_id, self.current_tenant)) raise exception_to_raise