This is an automated email from the ASF dual-hosted git repository. chinmayskulkarni pushed a commit to branch 4.14-HBase-1.3 in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.14-HBase-1.3 by this push: new e2c0b33 PHOENIX-5697 : Use try-with-resources to avoid leakage e2c0b33 is described below commit e2c0b33ee54799e8bf28fe9c3fa8206699e8bb2e 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 76ec32a..7f85aee 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 @@ -160,7 +160,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(); @@ -193,19 +192,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; }