Search in sources :

Example 16 with DatastoreLockTarget

use of com.b2international.snowowl.core.internal.locks.DatastoreLockTarget in project snow-owl by b2ihealthcare.

the class RepositoryTransactionContext method commit.

@Override
public Optional<Commit> commit(String author, String commitComment, String parentLockContext) {
    if (!isDirty()) {
        return Optional.empty();
    }
    // fall back to the current lock context or ROOT if none is present
    if (Strings.isNullOrEmpty(parentLockContext)) {
        parentLockContext = optionalService(Locks.class).map(Locks::lockContext).orElse(DatastoreLockContextDescriptions.ROOT);
    }
    final DatastoreLockContext lockContext = createLockContext(service(User.class).getUsername(), parentLockContext);
    final DatastoreLockTarget lockTarget = createLockTarget(info().id(), path());
    IOperationLockManager locks = service(IOperationLockManager.class);
    Commit commit = null;
    try {
        locks.lock(lockContext, 1000L, lockTarget);
        final long timestamp = service(TimestampProvider.class).getTimestamp();
        log().info("Persisting changes to {}@{}", path(), timestamp);
        commit = staging.commit(null, timestamp, author, commitComment);
        log().info("Changes have been successfully persisted to {}@{}.", path(), timestamp);
        return Optional.ofNullable(commit);
    } catch (final IndexException e) {
        Throwable rootCause = Throwables.getRootCause(e);
        if (rootCause instanceof CycleDetectedException) {
            throw (CycleDetectedException) rootCause;
        }
        throw new SnowowlRuntimeException(e.getMessage(), e);
    } finally {
        locks.unlock(lockContext, lockTarget);
        if (commit != null && isNotificationEnabled()) {
            service(RepositoryCommitNotificationSender.class).publish(this, commit);
        }
        clear();
    }
}
Also used : IndexException(com.b2international.index.IndexException) CycleDetectedException(com.b2international.commons.exceptions.CycleDetectedException) Locks(com.b2international.snowowl.core.locks.Locks) IOperationLockManager(com.b2international.snowowl.core.locks.IOperationLockManager) DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Aggregations

DatastoreLockTarget (com.b2international.snowowl.core.internal.locks.DatastoreLockTarget)16 DatastoreLockContext (com.b2international.snowowl.core.internal.locks.DatastoreLockContext)14 Test (org.junit.Test)5 Operation (io.swagger.v3.oas.annotations.Operation)4 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)4 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)2 IOperationLockManager (com.b2international.snowowl.core.locks.IOperationLockManager)2 CycleDetectedException (com.b2international.commons.exceptions.CycleDetectedException)1 IndexException (com.b2international.index.IndexException)1 BaseRevisionBranching (com.b2international.index.revision.BaseRevisionBranching)1 RevisionBranch (com.b2international.index.revision.RevisionBranch)1 Repository (com.b2international.snowowl.core.Repository)1 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)1 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)1 Locks (com.b2international.snowowl.core.locks.Locks)1 OverridingMethodsMustInvokeSuper (javax.annotation.OverridingMethodsMustInvokeSuper)1