ctubbsii commented on a change in pull request #341: ACCUMULO-3902 Ensure [Batch]Scanners are closed in ITs URL: https://github.com/apache/accumulo/pull/341#discussion_r157503827
########## File path: test/src/main/java/org/apache/accumulo/test/CleanWalIT.java ########## @@ -136,11 +136,13 @@ private int countLogs(String tableName, Connector conn) throws TableNotFoundExce log.debug("Saw {}={}", entry.getKey(), entry.getValue()); count++; } + scanner.close(); return count; } int count(String tableName, Connector conn) throws Exception { Scanner s = conn.createScanner(tableName, Authorizations.EMPTY); + s.close(); Review comment: This is a good example to highlight three different possibilities. In all three cases, the scanner instance never leaves the method (we never do `return s;` or `callOtherMethodToFinish(s);`), so we should always try to close it within the method. The first is the best because it guarantees the scanner is closed before the method returns, but also doesn't require all the complicated syntax of the old finally block in the second example. The third example is the worst, because it can leave the scanner unclosed. ```java // using try-with-resources try (Scanner s = conn.createScanner(tableName, Authorizations.EMPTY)) { return Iterators.size(s.iterator()); } ``` ```java // make sure it's closed, using old finally syntax Scanner s = null; try { s = conn.createScanner(tableName, Authorizations.EMPTY); return Iterators.size(s.iterator()); } finally { if (s != null) { s.close; } } ``` ```java // does not guarantee it is closed if it has an exception, but does try before it returns from the method Scanner s = conn.createScanner(tableName, Authorizations.EMPTY); int size = Iterators.size(s.iterator()); s.close(); return size; ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services