Open review DB in try-with-resource
Change-Id: I81357a357c9c2f589dab5801a435a475cf41da5b
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java
index d4312f6..edf3f6c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/ExportReviewNotes.java
@@ -76,17 +76,11 @@
}
private List<Change> allChanges() {
- ReviewDb db = null;
- try {
- db = database.open();
+ try (ReviewDb db = database.open()){
return db.changes().all().toList();
} catch (OrmException e) {
stderr.println("Cannot read changes from database " + e.getMessage());
return Collections.emptyList();
- } finally {
- if (db != null) {
- db.close();
- }
}
}
@@ -157,14 +151,7 @@
private class Worker extends Thread {
@Override
public void run() {
- ReviewDb db;
- try {
- db = database.open();
- } catch (OrmException e) {
- stderr.println(e.getMessage());
- return;
- }
- try {
+ try (ReviewDb db = database.open()){
for (;;) {
Entry<Project.NameKey, List<Change>> next = next();
if (next != null) {
@@ -177,9 +164,10 @@
break;
}
}
+ } catch (OrmException e) {
+ stderr.println(e.getMessage());
} finally {
monitor.endWorker();
- db.close();
}
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java
index c839c31..389195e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/RefUpdateListener.java
@@ -106,39 +106,21 @@
return;
}
- ReviewDb reviewDb;
- try {
- try {
- reviewDb = schema.open();
- } catch (OrmException x) {
- log.error(x.getMessage(), x);
- return;
+ try (ReviewDb reviewDb = schema.open()){
+ CreateReviewNotes crn = reviewNotesFactory.create(
+ reviewDb, projectName, git);
+ if (e.getRefName().startsWith("refs/heads/")) {
+ crn.createNotes(e.getRefName(),
+ ObjectId.fromString(e.getOldObjectId()),
+ ObjectId.fromString(e.getNewObjectId()),
+ null);
+ crn.commitNotes();
}
-
- try {
- CreateReviewNotes crn = reviewNotesFactory.create(
- reviewDb, projectName, git);
- if (e.getRefName().startsWith("refs/heads/")) {
- crn.createNotes(e.getRefName(),
- ObjectId.fromString(e.getOldObjectId()),
- ObjectId.fromString(e.getNewObjectId()),
- null);
- crn.commitNotes();
- }
- } catch (OrmException x) {
- log.error(x.getMessage(), x);
- } catch (IOException x) {
- log.error(x.getMessage(), x);
- } catch (ConcurrentRefUpdateException x) {
- log.error(x.getMessage(), x);
- } finally {
- reviewDb.close();
- }
-
+ } catch (OrmException | IOException | ConcurrentRefUpdateException x) {
+ log.error(x.getMessage(), x);
} finally {
git.close();
}
-
}
}