diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/patch/PatchScriptFactory.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/patch/PatchScriptFactory.java index 13a96a5f17..fada64fb15 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/patch/PatchScriptFactory.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/patch/PatchScriptFactory.java @@ -35,6 +35,7 @@ import com.google.gerrit.server.patch.PatchListKey; import com.google.gerrit.server.project.ChangeControl; import com.google.gerrit.server.project.NoSuchChangeException; import com.google.gwtorm.client.OrmException; +import com.google.gwtorm.client.SchemaFactory; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.assistedinject.Assisted; @@ -64,7 +65,7 @@ class PatchScriptFactory extends Handler { private final GitRepositoryManager repoManager; private final Provider builderFactory; private final PatchListCache patchListCache; - private final ReviewDb db; + private final SchemaFactory schemaFactory; private final ChangeControl.Factory changeControlFactory; private final Patch.Key patchKey; @@ -90,7 +91,7 @@ class PatchScriptFactory extends Handler { @Inject PatchScriptFactory(final GitRepositoryManager grm, Provider builderFactory, - final PatchListCache patchListCache, final ReviewDb db, + final PatchListCache patchListCache, final SchemaFactory sf, final ChangeControl.Factory changeControlFactory, @Assisted final Patch.Key patchKey, @Assisted("patchSetA") @Nullable final PatchSet.Id patchSetA, @@ -99,7 +100,7 @@ class PatchScriptFactory extends Handler { this.repoManager = grm; this.builderFactory = builderFactory; this.patchListCache = patchListCache; - this.db = db; + this.schemaFactory = sf; this.changeControlFactory = changeControlFactory; this.patchKey = patchKey; @@ -118,15 +119,7 @@ class PatchScriptFactory extends Handler { control = changeControlFactory.validateFor(changeId); change = control.getChange(); - patchSet = db.patchSets().get(patchSetId); - if (patchSet == null) { - throw new NoSuchChangeException(changeId); - } - - projectKey = change.getProject(); - aId = psa != null ? toObjectId(db, psa) : null; - bId = toObjectId(db, psb); - + final CommentDetail comments = allComments(); try { git = repoManager.openRepository(projectKey.get()); } catch (RepositoryNotFoundException e) { @@ -139,7 +132,6 @@ class PatchScriptFactory extends Handler { final PatchList list = listFor(keyFor(settings.getWhitespace())); final PatchScriptBuilder b = newBuilder(list); final PatchListEntry contentWS = list.get(fileName); - final CommentDetail comments = allComments(db); final PatchListEntry contentActual; if (settings.getWhitespace() == Whitespace.IGNORE_NONE) { @@ -220,19 +212,33 @@ class PatchScriptFactory extends Handler { } } - private CommentDetail allComments(final ReviewDb db) throws OrmException { - final CommentDetail r = new CommentDetail(psa, psb); - final String pn = patchKey.get(); - for (PatchLineComment p : db.patchComments().published(changeId, pn)) { - r.include(p); - } + private CommentDetail allComments() + throws OrmException, NoSuchChangeException { + ReviewDb db = schemaFactory.open(); + try { + final CommentDetail r = new CommentDetail(psa, psb); + patchSet = db.patchSets().get(patchSetId); + if (patchSet == null) { + throw new NoSuchChangeException(changeId); + } - if (control.getCurrentUser() instanceof IdentifiedUser) { - for (PatchLineComment p : db.patchComments().draft(changeId, pn, - ((IdentifiedUser) control.getCurrentUser()).getAccountId())) { + projectKey = change.getProject(); + aId = psa != null ? toObjectId(db, psa) : null; + bId = toObjectId(db, psb); + final String pn = patchKey.get(); + for (PatchLineComment p : db.patchComments().published(changeId, pn)) { r.include(p); } + + if (control.getCurrentUser() instanceof IdentifiedUser) { + for (PatchLineComment p : db.patchComments().draft(changeId, pn, + ((IdentifiedUser) control.getCurrentUser()).getAccountId())) { + r.include(p); + } + } + return r; + } finally { + db.close(); } - return r; } }