Open repository and database in try-with-resource

Change-Id: I46e179094806038dea4fa7108b52f0c4154ac009
diff --git a/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImporter.java b/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImporter.java
index 57cd1a3..25ff9e2 100644
--- a/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImporter.java
+++ b/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImporter.java
@@ -48,26 +48,27 @@
   public Account.Id importAccount(String login, String name, String email)
       throws IOException, BadRequestException, ResourceConflictException,
       UnprocessableEntityException, OrmException {
-    ReviewDb db = schema.get();
-    CreateAccount createAccount = createAccountFactory.create(login);
-    CreateAccount.Input accountInput = new CreateAccount.Input();
-    accountInput.email = email;
-    accountInput.username = login;
-    accountInput.name = MoreObjects.firstNonNull(name, login);
-    Response<AccountInfo> accountResponse =
-        (Response<AccountInfo>) createAccount.apply(TopLevelResource.INSTANCE,
-            accountInput);
-    if (accountResponse.statusCode() == HttpStatus.SC_CREATED) {
-      Id accountId = new Account.Id(accountResponse.value()._accountId);
-      db.accountExternalIds().insert(
-          Arrays
-              .asList(new AccountExternalId(accountId,
-                  new AccountExternalId.Key(AccountExternalId.SCHEME_GERRIT,
-                      login))));
-      return accountId;
-    } else {
-      throw new IOException("Cannot import GitHub account " + login
-          + ": HTTP Status " + accountResponse.statusCode());
+    try (ReviewDb db = schema.get()) {
+      CreateAccount createAccount = createAccountFactory.create(login);
+      CreateAccount.Input accountInput = new CreateAccount.Input();
+      accountInput.email = email;
+      accountInput.username = login;
+      accountInput.name = MoreObjects.firstNonNull(name, login);
+      Response<AccountInfo> accountResponse =
+          (Response<AccountInfo>) createAccount.apply(TopLevelResource.INSTANCE,
+              accountInput);
+      if (accountResponse.statusCode() == HttpStatus.SC_CREATED) {
+        Id accountId = new Account.Id(accountResponse.value()._accountId);
+        db.accountExternalIds().insert(
+            Arrays
+                .asList(new AccountExternalId(accountId,
+                    new AccountExternalId.Key(AccountExternalId.SCHEME_GERRIT,
+                        login))));
+        return accountId;
+      } else {
+        throw new IOException("Cannot import GitHub account " + login
+            + ": HTTP Status " + accountResponse.statusCode());
+      }
     }
   }
 }
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
index 8449edb..b0abb16 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
@@ -142,12 +142,10 @@
   private Map<String, List<GHPullRequest>> getPullRequests(GitHubLogin login,
       GHPerson ghOwner, Iterable<NameKey> repos) throws IOException {
     int numPullRequests = 0;
-    ReviewDb db = schema.get();
     Map<String, List<GHPullRequest>> allPullRequests = Maps.newHashMap();
-    try {
+    try (ReviewDb db = schema.get()) {
       for (NameKey gerritRepoName : repos) {
-        Repository gitRepo = repoMgr.openRepository(gerritRepoName);
-        try {
+        try (Repository gitRepo = repoMgr.openRepository(gerritRepoName)) {
           String ghRepoName = gerritRepoName.get().split("/")[1];
           Optional<GHRepository> githubRepo =
               getGHRepository(login, gerritRepoName);
@@ -156,13 +154,9 @@
                 collectPullRequestsFromGitHubRepository(numPullRequests, db,
                     allPullRequests, gitRepo, ghRepoName, githubRepo);
           }
-        } finally {
-          gitRepo.close();
         }
       }
       return allPullRequests;
-    } finally {
-      db.close();
     }
   }