com.orientechnologies.orient.core.exception.OConcurrentModificationException Java Examples

The following examples show how to use com.orientechnologies.orient.core.exception.OConcurrentModificationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeconflictStepTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private boolean tryConflictingUpdate(final TestEntity entity) {
  try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
    db.begin();

    ODocument copy = initialRecord.copy();
    entityAdapter.writeFields(copy, entity);
    copy.save();

    try {
      db.commit();
      return true;
    }
    catch (OConcurrentModificationException e) {
      logger.debug("Update denied due to conflict", e);
      return false;
    }
  }
  finally {
    try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
      entityAdapter.readFields(db.load(entityAdapter.recordIdentity(entity)), entity);
    }
  }
}
 
Example #2
Source File: DeconflictComponentMetadataTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private boolean tryUpdate(final Component component) {
  try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
    db.begin();

    ODocument copy = initialComponentRecord.copy();
    componentEntityAdapter.writeFields(copy, component);
    copy.save();

    try {
      db.commit();
      return true;
    }
    catch (OConcurrentModificationException e) {
      logger.debug("Update denied due to conflict", e);
      return false;
    }
  }
  finally {
    try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
      componentEntityAdapter.readFields(db.load(componentEntityAdapter.recordIdentity(component)), component);
    }
  }
}
 
Example #3
Source File: DeconflictAssetMetadataTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private boolean tryConflictingUpdate(final Asset asset) {
  try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
    db.begin();

    ODocument copy = initialAssetRecord.copy();
    assetEntityAdapter.writeFields(copy, asset);
    copy.save();

    try {
      db.commit();
      return true;
    }
    catch (OConcurrentModificationException e) {
      logger.debug("Update denied due to conflict", e);
      return false;
    }
  }
  finally {
    try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
      assetEntityAdapter.readFields(db.load(assetEntityAdapter.recordIdentity(asset)), asset);
    }
  }
}
 
Example #4
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void updateUser(final CUser user) throws UserNotFoundException {
  checkNotNull(user);
  checkNotNull(user.getId());
  checkUser(user);
  log.trace("Updating user: {}", user.getId());

  try {
    inTxRetry(databaseInstance).throwing(UserNotFoundException.class).run(db -> {
      CUser existing = userEntityAdapter.read(db, user.getId());
      if (existing == null) {
        throw new UserNotFoundException(user.getId());
      }
      userEntityAdapter.update(db, (OrientCUser) user);
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("User", user.getId(), e);
  }
}
 
Example #5
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean removeUser(final String id) {
  checkNotNull(id);
  log.trace("Removing user: {}", id);

  try {
    return inTxRetry(databaseInstance).call(db -> {
      if (userEntityAdapter.delete(db, id)) {
        removeUserRoleMapping(id, DEFAULT_SOURCE);
        return true;
      }
      return false;
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("User", id, e);
  }
}
 
Example #6
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void updatePrivilege(final CPrivilege privilege) {
  checkNotNull(privilege);
  checkNotNull(privilege.getId());
  checkPrivilege(privilege);
  log.trace("Updating privilege: {}", privilege.getId());

  try {
    inTxRetry(databaseInstance).run(db -> {
      CPrivilege existing = privilegeEntityAdapter.read(db, privilege.getId());
      if (existing == null) {
        throw new NoSuchPrivilegeException(privilege.getId());
      }
      privilegeEntityAdapter.update(db, (OrientCPrivilege) privilege);
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("Privilege", privilege.getId(), e);
  }
}
 
Example #7
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean removePrivilege(final String id) {
  checkNotNull(id);
  log.trace("Removing privilege: {}", id);

  try {
    return inTxRetry(databaseInstance).call(db -> {
      CPrivilege existing = privilegeEntityAdapter.read(db, id);
      if (existing == null) {
        throw new NoSuchPrivilegeException(id);
      }
      return privilegeEntityAdapter.delete(db, id);
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("Privilege", id, e);
  }
}
 
Example #8
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void updateRole(final CRole role) {
  checkNotNull(role);
  checkNotNull(role.getId());
  checkRole(role);
  log.trace("Updating role: {}", role.getId());

  try {
    inTxRetry(databaseInstance).run(db -> {
      CRole existing = roleEntityAdapter.read(db, role.getId());
      if (existing == null) {
        throw new NoSuchRoleException(role.getId());
      }
      if (!Objects.equals(role.getVersion(), existing.getVersion())) {
        throw concurrentlyModified("Role", role.getId(), null);
      }
      roleEntityAdapter.update(db, (OrientCRole) role);
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("Role", role.getId(), e);
  }
}
 
Example #9
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean removeRole(final String id) {
  checkNotNull(id);
  log.trace("Removing role: {}", id);

  try {
    return inTxRetry(databaseInstance).call(db -> {
      CRole existing = roleEntityAdapter.read(db, id);
      if (existing == null) {
        throw new NoSuchRoleException(id);
      }
      return roleEntityAdapter.delete(db, id);
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("Role", id, e);
  }
}
 
Example #10
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void updateUserRoleMapping(final CUserRoleMapping mapping) throws NoSuchRoleMappingException {
  checkNotNull(mapping);
  checkUserRoleMapping(mapping);
  checkNotNull(mapping.getUserId());
  checkNotNull(mapping.getSource());
  log.trace("Updating user/role mappings for: {}/{}", mapping.getUserId(), mapping.getSource());

  try {
    inTxRetry(databaseInstance).throwing(NoSuchRoleMappingException.class).run(db -> {
      CUserRoleMapping existing = userRoleMappingEntityAdapter.read(db, mapping.getUserId(), mapping.getSource());
      if (existing == null) {
        throw new NoSuchRoleMappingException(mapping.getUserId());
      }
      if (!Objects.equals(mapping.getVersion(), existing.getVersion())) {
        throw concurrentlyModified("User-role mapping", mapping.getUserId(), null);
      }
      userRoleMappingEntityAdapter.update(db, (OrientCUserRoleMapping) mapping);
    });
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("User-role mapping", mapping.getUserId(), e);
  }
}
 
Example #11
Source File: OLuceneClassIndexManager.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
@Override
public RESULT onRecordBeforeDelete(final ODocument iDocument) {
  final ORecordVersion version = iDocument.getRecordVersion(); // Cache the transaction-provided value
  if (iDocument.fields() == 0) {
    // FORCE LOADING OF CLASS+FIELDS TO USE IT AFTER ON onRecordAfterDelete METHOD
    iDocument.reload();
    if (version.getCounter() > -1 && iDocument.getRecordVersion().compareTo(version) != 0) // check for record version errors
      if (OFastConcurrentModificationException.enabled())
        throw OFastConcurrentModificationException.instance();
      else
        throw new OConcurrentModificationException(iDocument.getIdentity(), iDocument.getRecordVersion(), version,
            ORecordOperation.DELETED);
  }

  return RESULT.RECORD_NOT_CHANGED;
}
 
Example #12
Source File: OTaskSession.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
public void atomicChange(final String field, final Object value,final String changeCommand){
	new DBClosure<Boolean>() {
		@Override
		protected Boolean execute(ODatabaseDocument db) {
			int maxRetries = 50;
			OCommandSQL command = new OCommandSQL("update "+document.getIdentity()+" "+changeCommand);
			int retry = 0;					
			while(true){
				try {
					command.execute(value);
					break;
				} catch (OConcurrentModificationException  e) {
					retry++;
					try { Thread.sleep((long) (Math.random()*150));} catch (InterruptedException e1) {}
					if (retry>=maxRetries){
						throw e;//if all retries failed
					}
				}
			}
			document.reload();
			return true;
		}
	}.execute();
}
 
Example #13
Source File: OrientSecurityConfigurationSource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean removeUserRoleMapping(final String userId, final String source) {
  checkNotNull(userId);
  checkNotNull(source);
  log.trace("Removing user/role mappings for: {}/{}", userId, source);

  try {
    return inTxRetry(databaseInstance).call(db -> userRoleMappingEntityAdapter.delete(db, userId, source));
  }
  catch (OConcurrentModificationException e) {
    throw concurrentlyModified("User-role mapping", userId, e);
  }
}
 
Example #14
Source File: ConflictHook.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Attempts to resolve the potential conflict by delegating to the resolving entity adapter.
 */
@Override
@Nullable
public byte[] onUpdate(final OStorage storage,
                       final byte recordType,
                       final ORecordId rid,
                       final int recordVersion,
                       final byte[] changeContent,
                       final AtomicInteger dbVersion)
{
  // most records won't have an entity adapter interested in resolving their conflicts
  Optional<EntityAdapter<?>> adapter = findResolvingAdapter(storage, rid.getClusterId());
  if (adapter.isPresent()) {

    // attempt to load the current stored record content
    byte[] storedContent = storage.readRecord(rid, null, false, false, null).getResult().getBuffer();

    ConflictState state;
    ODocument changeRecord = null;

    if (recordType == RECORD_TYPE) {
      // turn the stored content into a proper record
      ODocument storedRecord = new ODocument(rid).fromStream(storedContent);

      // retrieve the change we originally wanted to save
      changeRecord = getChangeRecord(rid, changeContent);

      // delegate conflict resolution to owning entity adapter
      state = adapter.get().resolve(storedRecord, changeRecord);

      log.trace("{} update of {} with {}", state, storedRecord, changeRecord);
    }
    else {
      // binary content - no merging, we can only do a simple comparison
      state = Arrays.equals(storedContent, changeContent) ? IGNORE : DENY;

      log.trace("{} binary update of {}", state, rid);
    }

    switch (state) {
      case IGNORE:
        // for now treat "no-op" changes like ALLOW, but without any version bump
        return null;
      case ALLOW:
        // go ahead with original change, but bump version if record was behind DB
        dbVersion.set(max(dbVersion.get() + 1, recordVersion));
        return null;
      case MERGE:
        // return merged content and bump version whether record was behind or not
        dbVersion.set(max(dbVersion.get(), recordVersion) + 1);
        return ofNullable(changeRecord).map(ODocument::toStream).orElse(null);
      default:
        break;
    }
  }

  throw new OConcurrentModificationException(rid, dbVersion.get(), recordVersion, UPDATED);
}