private void releaseLocks() {
    if (lockElements != null) {
      Collection<LockElement> releaseFailures = null;
      Exception releaseException = null;
      for (LockElement lockElement : lockElements) {
        try {
          lockElement.releaseIfAcquired();
        } catch (Exception e) {
          releaseException = e;
          if (releaseFailures == null) {
            releaseFailures = new ArrayList<>();
          }
          releaseFailures.add(lockElement);
        }
      }

      if (releaseException != null) {
        log.warn(
            "Unable to release locks: "
                + releaseFailures
                + ". Example of exception:"
                + releaseException);
      }
    }
  }
Example #2
0
  public <T extends PropertyContainer> T indexPutIfAbsent(
      Index<T> index, T entity, String key, Object value) {
    T existing = index.get(key, value).getSingle();
    if (existing != null) {
      return existing;
    }

    // Grab lock
    IndexLock lock = new IndexLock(index.getName(), key);
    TransactionState state = getTransactionState();
    LockElement writeLock = state.acquireWriteLock(lock);

    // Check again -- now holding the lock
    existing = index.get(key, value).getSingle();
    if (existing != null) {
      // Someone else created this entry, release the lock as we won't be needing it
      writeLock.release();
      return existing;
    }

    // Add
    index.add(entity, key, value);
    return null;
  }