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; }