You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ha...@apache.org on 2020/10/21 03:17:46 UTC

[zookeeper] branch master updated: ZOOKEEPER-3971: Auto close resources with try catch block

This is an automated email from the ASF dual-hosted git repository.

hanm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new f06db8c  ZOOKEEPER-3971: Auto close resources with try catch block
f06db8c is described below

commit f06db8c91becf812c539747a406ba84635b2c1ac
Author: kamaci <fu...@gmail.com>
AuthorDate: Tue Oct 20 20:17:34 2020 -0700

    ZOOKEEPER-3971: Auto close resources with try catch block
    
    Author: kamaci <fu...@gmail.com>
    
    Reviewers: Michael Han <ha...@apache.org>, Enrico Olivelli <eo...@gmail.com>, Christopher Tubbs
    
    Closes #1507 from kamaci/ZOOKEEPER-3971
---
 .../zookeeper/server/persistence/FileTxnLog.java   | 23 ++--------------------
 1 file changed, 2 insertions(+), 21 deletions(-)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java
index 62969ba..366d502 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java
@@ -354,10 +354,7 @@ public class FileTxnLog implements TxnLog, Closeable {
         // if a log file is more recent we must scan it to find
         // the highest zxid
         long zxid = maxLog;
-        TxnIterator itr = null;
-        try {
-            FileTxnLog txn = new FileTxnLog(logDir);
-            itr = txn.read(maxLog);
+        try (FileTxnLog txn = new FileTxnLog(logDir); TxnIterator itr = txn.read(maxLog)) {
             while (true) {
                 if (!itr.next()) {
                     break;
@@ -367,22 +364,10 @@ public class FileTxnLog implements TxnLog, Closeable {
             }
         } catch (IOException e) {
             LOG.warn("Unexpected exception", e);
-        } finally {
-            close(itr);
         }
         return zxid;
     }
 
-    private void close(TxnIterator itr) {
-        if (itr != null) {
-            try {
-                itr.close();
-            } catch (IOException ioe) {
-                LOG.warn("Error closing file iterator", ioe);
-            }
-        }
-    }
-
     /**
      * commit the logs. make sure that everything hits the
      * disk
@@ -468,9 +453,7 @@ public class FileTxnLog implements TxnLog, Closeable {
      * @return true if successful false if not
      */
     public boolean truncate(long zxid) throws IOException {
-        FileTxnIterator itr = null;
-        try {
-            itr = new FileTxnIterator(this.logDir, zxid);
+        try (FileTxnIterator itr = new FileTxnIterator(this.logDir, zxid)) {
             PositionInputStream input = itr.inputStream;
             if (input == null) {
                 throw new IOException("No log files found to truncate! This could "
@@ -487,8 +470,6 @@ public class FileTxnLog implements TxnLog, Closeable {
                     LOG.warn("Unable to truncate {}", itr.logFile);
                 }
             }
-        } finally {
-            close(itr);
         }
         return true;
     }