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

chinmayskulkarni pushed a commit to branch 4.14-HBase-1.4
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.14-HBase-1.4 by this push:
     new b6b7d5e  PHOENIX-5697 : Use try-with-resources to avoid leakage
b6b7d5e is described below

commit b6b7d5ed201d1109e468929f8f8bb8c0f79cd7bb
Author: Viraj Jasani <vjas...@apache.org>
AuthorDate: Fri Jan 24 11:12:28 2020 -0800

    PHOENIX-5697 : Use try-with-resources to avoid leakage
    
    Signed-off-by: Chinmay Kulkarni <chinmayskulka...@apache.org>
---
 .../write/AbstractParallelWriterIndexCommitter.java    | 13 ++++---------
 .../write/TrackingParallelWriterIndexCommitter.java    | 12 ++++--------
 .../java/org/apache/phoenix/util/MetaDataUtil.java     | 18 ++++--------------
 3 files changed, 12 insertions(+), 31 deletions(-)

diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/AbstractParallelWriterIndexCommitter.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/AbstractParallelWriterIndexCommitter.java
index 9e94e87..2fd70de 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/AbstractParallelWriterIndexCommitter.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/AbstractParallelWriterIndexCommitter.java
@@ -143,7 +143,6 @@ public abstract class AbstractParallelWriterIndexCommitter 
implements IndexCommi
                 @SuppressWarnings("deprecation")
                 @Override
                 public Void call() throws Exception {
-                    Table table = null;
                     // this may have been queued, so another task infront of 
us may have failed, so we should
                     // early exit, if that's the case
                     throwFailureIfDone();
@@ -176,9 +175,10 @@ public abstract class AbstractParallelWriterIndexCommitter 
implements IndexCommi
                         else {
                             factory = retryingFactory;
                         }
-                        table = factory.getTable(tableReference.get());
-                        throwFailureIfDone();
-                        table.batch(mutations, null);
+                        try (Table table = 
factory.getTable(tableReference.get())) {
+                            throwFailureIfDone();
+                            table.batch(mutations, null);
+                        }
                     } catch (SingleIndexWriteFailureException e) {
                         throw e;
                     } catch (IOException e) {
@@ -188,11 +188,6 @@ public abstract class AbstractParallelWriterIndexCommitter 
implements IndexCommi
                         Thread.currentThread().interrupt();
                         throw new 
SingleIndexWriteFailureException(tableReference.toString(), mutations, e, 
PhoenixIndexFailurePolicy.getDisableIndexOnFailure(env));
                     }
-                    finally{
-                        if (table != null) {
-                            table.close();
-                        }
-                    }
                     return null;
                 }
 
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/TrackingParallelWriterIndexCommitter.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/TrackingParallelWriterIndexCommitter.java
index 072bcae..dba25e3 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/TrackingParallelWriterIndexCommitter.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/write/TrackingParallelWriterIndexCommitter.java
@@ -159,7 +159,6 @@ public class TrackingParallelWriterIndexCommitter 
implements IndexCommitter {
                 @SuppressWarnings("deprecation")
                 @Override
                 public Boolean call() throws Exception {
-                    HTableInterface table = null;
                     try {
                         // this may have been queued, but there was an 
abort/stop so we try to early exit
                         throwFailureIfDone();
@@ -192,19 +191,16 @@ public class TrackingParallelWriterIndexCommitter 
implements IndexCommitter {
                         else {
                             factory = retryingFactory;
                         }
-                        table = factory.getTable(tableReference.get());
-                        throwFailureIfDone();
-                        table.batch(mutations);
+                        try (HTableInterface table = 
factory.getTable(tableReference.get())) {
+                          throwFailureIfDone();
+                          table.batch(mutations);
+                        }
                     } catch (InterruptedException e) {
                         // reset the interrupt status on the thread
                         Thread.currentThread().interrupt();
                         throw e;
                     } catch (Exception e) {
                         throw e;
-                    } finally {
-                        if (table != null) {
-                            table.close();
-                        }
                     }
                     return Boolean.TRUE;
                 }
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java 
b/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
index 0c1f365..3cc5c04 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java
@@ -531,12 +531,10 @@ public class MetaDataUtil {
      * @throws
      */
     public static boolean tableRegionsOnline(Configuration conf, PTable table) 
{
-        HConnection hcon = null;
-
-        try {
-            hcon = HConnectionManager.getConnection(conf);
+        try (HConnection hcon =
+               HConnectionManager.getConnection(conf)) {
             List<HRegionLocation> locations = hcon.locateRegions(
-                
org.apache.hadoop.hbase.TableName.valueOf(table.getPhysicalName().getBytes()));
+              
org.apache.hadoop.hbase.TableName.valueOf(table.getPhysicalName().getBytes()));
 
             for (HRegionLocation loc : locations) {
                 try {
@@ -558,17 +556,9 @@ public class MetaDataUtil {
                 }
             }
         } catch (IOException ex) {
-            LOGGER.warn("tableRegionsOnline failed due to:" + ex);
+            LOGGER.warn("tableRegionsOnline failed due to:", ex);
             return false;
-        } finally {
-            if (hcon != null) {
-                try {
-                    hcon.close();
-                } catch (IOException ignored) {
-                }
-            }
         }
-
         return true;
     }
 

Reply via email to