[spotbugs] DfsReftableDatabase: extract lock to local variable

This fixes UL_UNRELEASED_LOCK_EXCEPTION_PATH raised by spotbugs in
#DfsReftableDatabase and #clearCache.

Change-Id: Ifd3189288d2a8e64139c02cd105eb335fa2f68cf
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2020-12-04 11:12:27 +01:00
parent 1fd0a49ce0
commit 39cbc574d1
1 changed files with 7 additions and 4 deletions

View File

@ -17,6 +17,7 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.annotations.Nullable;
@ -62,11 +63,12 @@ protected DfsReftableDatabase(DfsRepository repo) {
reftableDatabase = new ReftableDatabase() { reftableDatabase = new ReftableDatabase() {
@Override @Override
public MergedReftable openMergedReftable() throws IOException { public MergedReftable openMergedReftable() throws IOException {
DfsReftableDatabase.this.getLock().lock(); Lock l = DfsReftableDatabase.this.getLock();
l.lock();
try { try {
return new MergedReftable(stack().readers()); return new MergedReftable(stack().readers());
} finally { } finally {
DfsReftableDatabase.this.getLock().unlock(); l.unlock();
} }
} }
}; };
@ -207,7 +209,8 @@ boolean exists() throws IOException {
@Override @Override
void clearCache() { void clearCache() {
getLock().lock(); ReentrantLock l = getLock();
l.lock();
try { try {
if (ctx != null) { if (ctx != null) {
ctx.close(); ctx.close();
@ -219,7 +222,7 @@ void clearCache() {
stack = null; stack = null;
} }
} finally { } finally {
getLock().unlock(); l.unlock();
} }
} }