RDBDocumentStore: use try-with-resources for nodes cache locks
@@ -587,8 +587,7 @@ public class RDBDocumentStore implements DocumentStore {
}
private void invalidateNodesCache(String id, boolean remove) {
- Lock lock = locks.acquire(id);
- try {
+ try (CacheLock lock = acquireLockFor(id)) {
if (remove) {
nodesCache.invalidate(id);
} else {
@@ -598,8 +597,6 @@ public class RDBDocumentStore implements DocumentStore {
entry.markUpToDate(0);
}
}
- } finally {
- lock.unlock();
}
}
@@ -1374,8 +1371,7 @@ public class RDBDocumentStore implements DocumentStore {
}
}
try {
- Lock lock = locks.acquire(id);
- try {
+ try (CacheLock lock = acquireLockFor(id)) {
// caller really wants the cache to be cleared
if (maxCacheAge == 0) {
invalidateNodesCache(id, true);
@@ -1408,8 +1404,6 @@ public class RDBDocumentStore implements DocumentStore {
doc = wrap(ndoc);
nodesCache.put(doc);
}
- } finally {
- lock.unlock();
}
return castAsT(unwrap(doc));
} catch (ExecutionException e) {
@@ -1523,12 +1517,10 @@ public class RDBDocumentStore implements DocumentStore {
maintainUpdateStats(collection, update.getId());
addUpdateCounters(update);
T doc = createNewDocument(collection, oldDoc, update);
- Lock l = locks.acquire(update.getId());
final Stopwatch watch = startWatch();
boolean success = false;
int retries = maxRetries;
- try {
-
+ try (CacheLock lock = acquireLockFor(update.getId())) {
while (!success && retries > 0) {
long lastmodcount = modcountOf(oldDoc);
success = updateDocument(collection, doc, update, lastmodcount);
@@ -1571,7 +1563,6 @@ public class RDBDocumentStore implements DocumentStore {
return oldDoc;
} finally {
- l.unlock();
int numOfAttempts = maxRetries - retries - 1;
stats.doneFindAndModify(watch.elapsed(TimeUnit.NANOSECONDS), collection,
update.getId(), false, success, numOfAttempts);
@@ -1642,14 +1633,11 @@ public class RDBDocumentStore implements DocumentStore {
// and a tracker is present
long lastmodified = modifiedOf(doc);
if (lastmodified == row.getModified() && lastmodified >= 1) {
- Lock lock = locks.acquire(row.getId());
- try {
+ try (CacheLock lock = acquireLockFor(row.getId())) {
if (!tracker.mightBeenAffected(row.getId())) {
// otherwise mark it as fresh
((NodeDocument) doc).markUpToDate(now);
}
- } finally {
- lock.unlock();
}
}
else {
@@ -2233,6 +2221,24 @@ public class RDBDocumentStore implements DocumentStore {
}
}
+ private CacheLock acquireLockFor(String id) {
+ return new CacheLock(this.locks, id);
+ }
+
+ private static class CacheLock implements AutoCloseable {
+
+ private final Lock lock;
+
+ public CacheLock(NodeDocumentLocks locks, String id) {
+ this.lock = locks.acquire(id);
+ }
+
+ @Override
+ public void close() {
+ lock.unlock();
+ }
+ }
+
// slightly extended query support
protected static class QueryCondition {
