Merge pull request #11412 from s1monw/close_lock

Close lock even if we fail to obtain
This commit is contained in:
Simon Willnauer 2015-05-29 13:37:54 +02:00
commit a7779d8e1d
1 changed files with 10 additions and 2 deletions

View File

@ -189,8 +189,16 @@ public class Lucene {
@SuppressForbidden(reason = "this method uses trappy Directory#makeLock API") @SuppressForbidden(reason = "this method uses trappy Directory#makeLock API")
public static Lock acquireLock(Directory directory, String lockName, long timeout) throws IOException { public static Lock acquireLock(Directory directory, String lockName, long timeout) throws IOException {
final Lock writeLock = directory.makeLock(lockName); final Lock writeLock = directory.makeLock(lockName);
if (writeLock.obtain(timeout) == false) { boolean success = false;
throw new LockObtainFailedException("failed to obtain lock: " + writeLock); try {
if (writeLock.obtain(timeout) == false) {
throw new LockObtainFailedException("failed to obtain lock: " + writeLock);
}
success = true;
} finally {
if (success == false) {
writeLock.close();
}
} }
return writeLock; return writeLock;
} }