Skip to content

Commit c64dc8a

Browse files
authored
Using KeyValueStorage.Batch more rigorously (#4576)
Every time use KeyValueStorage.Batch, close it in finally
1 parent 2b5b992 commit c64dc8a

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndex.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ private long getLastEntryInLedgerInternal(long ledgerId) throws IOException {
147147
}
148148

149149
public void addLocation(long ledgerId, long entryId, long location) throws IOException {
150-
Batch batch = locationsDb.newBatch();
151-
addLocation(batch, ledgerId, entryId, location);
152-
batch.flush();
153-
batch.close();
150+
try (Batch batch = locationsDb.newBatch()) {
151+
addLocation(batch, ledgerId, entryId, location);
152+
batch.flush();
153+
}
154154
}
155155

156156
public Batch newBatch() {
@@ -178,18 +178,17 @@ public void updateLocations(Iterable<EntryLocation> newLocations) throws IOExcep
178178
log.debug("Update locations -- {}", Iterables.size(newLocations));
179179
}
180180

181-
Batch batch = newBatch();
182-
// Update all the ledger index pages with the new locations
183-
for (EntryLocation e : newLocations) {
184-
if (log.isDebugEnabled()) {
185-
log.debug("Update location - ledger: {} -- entry: {}", e.ledger, e.entry);
186-
}
181+
try (Batch batch = newBatch()) {
182+
// Update all the ledger index pages with the new locations
183+
for (EntryLocation e : newLocations) {
184+
if (log.isDebugEnabled()) {
185+
log.debug("Update location - ledger: {} -- entry: {}", e.ledger, e.entry);
186+
}
187187

188-
addLocation(batch, e.ledger, e.entry, e.location);
188+
addLocation(batch, e.ledger, e.entry, e.location);
189+
}
190+
batch.flush();
189191
}
190-
191-
batch.flush();
192-
batch.close();
193192
}
194193

195194
public void delete(long ledgerId) throws IOException {

0 commit comments

Comments
 (0)