Uses auto-closable ReviewDb and Repository
Transaction is now completely managed by the inner BatchOp and
the only thing we need to make sure is that ReviewDb and Repository
are closed at the end of the block.
Change-Id: Ifea052dae25fd219f91b65d5691e086f59182c2c
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
index e17044a..d49bb1b 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
@@ -125,17 +125,15 @@
@Override
public void run() {
- ReviewDb db = schema.get();
- try {
+ try (ReviewDb db = schema.get()) {
status.update(GitJobStatus.Code.SYNC);
exitWhenCancelled();
GHPullRequest pr = fetchGitHubPullRequestInfo();
exitWhenCancelled();
- Repository gitRepo =
+ try (Repository gitRepo =
repoMgr.openRepository(new Project.NameKey(organisation + "/"
- + repoName));
- try {
+ + repoName))) {
exitWhenCancelled();
fetchGitHubPullRequest(gitRepo, pr);
@@ -143,28 +141,13 @@
List<Id> changeIds = addPullRequestToChange(db, pr, gitRepo);
status.update(GitJobStatus.Code.COMPLETE, "Imported",
"PullRequest imported as Changes " + changeIds);
- } finally {
- gitRepo.close();
}
- db.commit();
} catch (JobCancelledException e) {
status.update(GitJobStatus.Code.CANCELLED);
- try {
- db.rollback();
- } catch (OrmException e1) {
- LOG.error("Error rolling back transation", e1);
- }
} catch (Throwable e) {
LOG.error("Pull request " + prId + " into repository " + organisation
+ "/" + repoName + " was failed", e);
status.update(GitJobStatus.Code.FAILED, "Failed", e.getLocalizedMessage());
- try {
- db.rollback();
- } catch (OrmException e1) {
- LOG.error("Error rolling back transation", e1);
- }
- } finally {
- db.close();
}
}