This is an automated email from the ASF dual-hosted git repository. vjasani pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/master by this push: new cb82e60f98 PHOENIX-7316 Need close more Statements (#1917) cb82e60f98 is described below commit cb82e60f98dce8922973e71a1f06d90165125b25 Author: chaijunjie0101 <64140218+chaijunjie0...@users.noreply.github.com> AuthorDate: Thu Jun 27 05:36:41 2024 +0800 PHOENIX-7316 Need close more Statements (#1917) --- .../java/org/apache/phoenix/schema/task/Task.java | 23 ++-- .../apache/phoenix/trace/PhoenixMetricsSink.java | 8 +- .../java/org/apache/phoenix/trace/TraceReader.java | 146 ++++++++++----------- .../java/org/apache/phoenix/trace/TraceWriter.java | 3 +- .../index/automation/PhoenixMRJobSubmitter.java | 27 ++-- 5 files changed, 104 insertions(+), 103 deletions(-) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/task/Task.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/task/Task.java index 0620e625f9..bd1d912b03 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/task/Task.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/task/Task.java @@ -161,10 +161,11 @@ public class Task { try (PhoenixConnection newConnection = QueryUtil.getConnectionOnServer(curConn.getClientInfo(), conf) .unwrap(PhoenixConnection.class)) { - PreparedStatement statement = addTaskAndGetStatement( - systemTaskParams, newConnection); - return executeStatementAndGetTaskMutations(newConnection, - statement); + try (PreparedStatement statement = addTaskAndGetStatement( + systemTaskParams, newConnection)) { + return executeStatementAndGetTaskMutations(newConnection, + statement); + } } } @@ -202,14 +203,14 @@ public class Task { private static List<TaskRecord> populateTasks(Connection connection, String taskQuery) throws SQLException { - PreparedStatement taskStatement = connection.prepareStatement(taskQuery); - ResultSet rs = taskStatement.executeQuery(); - List<TaskRecord> result = new ArrayList<>(); - while (rs.next()) { - // delete child views only if the parent table is deleted from the system catalog - TaskRecord taskRecord = parseResult(rs); - result.add(taskRecord); + try (PreparedStatement taskStatement = connection.prepareStatement(taskQuery); + ResultSet rs = taskStatement.executeQuery()) { + while (rs.next()) { + // delete child views only if the parent table is deleted from the system catalog + TaskRecord taskRecord = parseResult(rs); + result.add(taskRecord); + } } return result; } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/trace/PhoenixMetricsSink.java b/phoenix-core-client/src/main/java/org/apache/phoenix/trace/PhoenixMetricsSink.java index e704763dae..cc672a0bcc 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/trace/PhoenixMetricsSink.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/trace/PhoenixMetricsSink.java @@ -199,8 +199,9 @@ public class PhoenixMetricsSink implements MetricsSink { // tables created as transactional tables, make these table non // transactional PhoenixDatabaseMetaData.TRANSACTIONAL + "=" + Boolean.FALSE; - PreparedStatement stmt = conn.prepareStatement(ddl); - stmt.execute(); + try (PreparedStatement stmt = conn.prepareStatement(ddl)) { + stmt.execute(); + } } @Override @@ -288,8 +289,7 @@ public class PhoenixMetricsSink implements MetricsSink { LOGGER.trace("Logging metrics to phoenix table via: " + stmt); LOGGER.trace("With tags: " + variableValues); } - try { - PreparedStatement ps = conn.prepareStatement(stmt); + try (PreparedStatement ps = conn.prepareStatement(stmt)) { // add everything that wouldn't/may not parse int index = 1; for (String tag : variableValues) { diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceReader.java b/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceReader.java index 9fc001876d..1b93000588 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceReader.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceReader.java @@ -18,6 +18,7 @@ package org.apache.phoenix.trace; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -88,90 +89,89 @@ public class TraceReader { + " ORDER BY " + MetricInfo.TRACE.columnName + " DESC, " + MetricInfo.START.columnName + " ASC" + " LIMIT " + pageSize; int resultCount = 0; - ResultSet results = conn.prepareStatement(query).executeQuery(); - TraceHolder trace = null; - // the spans that are not the root span, but haven't seen their parent yet - List<SpanInfo> orphans = null; - while (results.next()) { - int index = 1; - long traceid = results.getLong(index++); - long parent = results.getLong(index++); - long span = results.getLong(index++); - String desc = results.getString(index++); - long start = results.getLong(index++); - long end = results.getLong(index++); - String host = results.getString(index++); - int tagCount = results.getInt(index++); - int annotationCount = results.getInt(index++); - // we have a new trace - if (trace == null || traceid != trace.traceid) { - // only increment if we are on a new trace, to ensure we get at least one - if (trace != null) { - resultCount++; - } - // we beyond the limit, so we stop - if (resultCount >= limit) { - break; + try (PreparedStatement stmt = conn.prepareStatement(query); + ResultSet results = stmt.executeQuery()) { + TraceHolder trace = null; + // the spans that are not the root span, but haven't seen their parent yet + List<SpanInfo> orphans = null; + while (results.next()) { + int index = 1; + long traceid = results.getLong(index++); + long parent = results.getLong(index++); + long span = results.getLong(index++); + String desc = results.getString(index++); + long start = results.getLong(index++); + long end = results.getLong(index++); + String host = results.getString(index++); + int tagCount = results.getInt(index++); + int annotationCount = results.getInt(index++); + // we have a new trace + if (trace == null || traceid != trace.traceid) { + // only increment if we are on a new trace, to ensure we get at least one + if (trace != null) { + resultCount++; + } + // we beyond the limit, so we stop + if (resultCount >= limit) { + break; + } + trace = new TraceHolder(); + // add the orphans, so we can track them later + orphans = new ArrayList<SpanInfo>(); + trace.orphans = orphans; + trace.traceid = traceid; + traces.add(trace); } - trace = new TraceHolder(); - // add the orphans, so we can track them later - orphans = new ArrayList<SpanInfo>(); - trace.orphans = orphans; - trace.traceid = traceid; - traces.add(trace); - } - // search the spans to determine the if we have a known parent - SpanInfo parentSpan = null; - if (parent != Span.ROOT_SPAN_ID) { - // find the parent - for (SpanInfo p : trace.spans) { - if (p.id == parent) { - parentSpan = p; - break; + // search the spans to determine the if we have a known parent + SpanInfo parentSpan = null; + if (parent != Span.ROOT_SPAN_ID) { + // find the parent + for (SpanInfo p : trace.spans) { + if (p.id == parent) { + parentSpan = p; + break; + } } } - } - SpanInfo spanInfo = - new SpanInfo(parentSpan, parent, span, desc, start, end, host, tagCount, - annotationCount); - // search the orphans to see if this is the parent id - - for (int i = 0; i < orphans.size(); i++) { - SpanInfo orphan = orphans.get(i); - // we found the parent for the orphan - if (orphan.parentId == span) { - // update the bi-directional relationship - orphan.parent = spanInfo; - spanInfo.children.add(orphan); - // / its no longer an orphan - LOGGER.trace(addCustomAnnotations("Found parent for span: " + span)); - orphans.remove(i--); + SpanInfo spanInfo = + new SpanInfo(parentSpan, parent, span, desc, start, end, host, tagCount, + annotationCount); + // search the orphans to see if this is the parent id + + for (int i = 0; i < orphans.size(); i++) { + SpanInfo orphan = orphans.get(i); + // we found the parent for the orphan + if (orphan.parentId == span) { + // update the bi-directional relationship + orphan.parent = spanInfo; + spanInfo.children.add(orphan); + // / its no longer an orphan + LOGGER.trace(addCustomAnnotations("Found parent for span: " + span)); + orphans.remove(i--); + } } - } - if (parentSpan != null) { - // add this as a child to the parent span - parentSpan.children.add(spanInfo); - } else if (parent != Span.ROOT_SPAN_ID) { - // add the span to the orphan pile to check for the remaining spans we see - LOGGER.info(addCustomAnnotations("No parent span found for span: " + span + " (root span id: " - + Span.ROOT_SPAN_ID + ")")); - orphans.add(spanInfo); - } + if (parentSpan != null) { + // add this as a child to the parent span + parentSpan.children.add(spanInfo); + } else if (parent != Span.ROOT_SPAN_ID) { + // add the span to the orphan pile to check for the remaining spans we see + LOGGER.info(addCustomAnnotations("No parent span found for span: " + + span + " (root span id: " + Span.ROOT_SPAN_ID + ")")); + orphans.add(spanInfo); + } - // add the span to the full known list - trace.spans.add(spanInfo); + // add the span to the full known list + trace.spans.add(spanInfo); - // go back and find the tags for the row - spanInfo.tags.addAll(getTags(traceid, parent, span, tagCount)); + // go back and find the tags for the row + spanInfo.tags.addAll(getTags(traceid, parent, span, tagCount)); - spanInfo.annotations.addAll(getAnnotations(traceid, parent, span, annotationCount)); + spanInfo.annotations.addAll(getAnnotations(traceid, parent, span, annotationCount)); + } } - // make sure we clean up after ourselves - results.close(); - return traces; } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceWriter.java b/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceWriter.java index a6747968b3..1d2b75f188 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceWriter.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/trace/TraceWriter.java @@ -220,8 +220,7 @@ public class TraceWriter { LOGGER.trace("Logging metrics to phoenix table via: " + stmt); LOGGER.trace("With tags: " + variableValues); } - try { - PreparedStatement ps = conn.prepareStatement(stmt); + try (PreparedStatement ps = conn.prepareStatement(stmt)) { // add everything that wouldn't/may not parse int index = 1; for (String tag : variableValues) { diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/mapreduce/index/automation/PhoenixMRJobSubmitter.java b/phoenix-core-server/src/main/java/org/apache/phoenix/mapreduce/index/automation/PhoenixMRJobSubmitter.java index 93fa57c49f..e904b3f931 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/mapreduce/index/automation/PhoenixMRJobSubmitter.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/mapreduce/index/automation/PhoenixMRJobSubmitter.java @@ -210,20 +210,21 @@ public class PhoenixMRJobSubmitter { public Map<String, PhoenixAsyncIndex> getCandidateJobs(Connection con) throws SQLException { Properties props = new Properties(); UpgradeUtil.doNotUpgradeOnFirstConnection(props); - Statement s = con.createStatement(); - ResultSet rs = s.executeQuery(CANDIDATE_INDEX_INFO_QUERY); - Map<String, PhoenixAsyncIndex> candidateIndexes = new HashMap<String, PhoenixAsyncIndex>(); - while (rs.next()) { - PhoenixAsyncIndex indexInfo = new PhoenixAsyncIndex(); - indexInfo.setIndexType(IndexType.fromSerializedValue(rs - .getByte(PhoenixDatabaseMetaData.INDEX_TYPE))); - indexInfo.setDataTableName(rs.getString(PhoenixDatabaseMetaData.DATA_TABLE_NAME)); - indexInfo.setTableSchem(rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM)); - indexInfo.setTableName(rs.getString(PhoenixDatabaseMetaData.TABLE_NAME)); - candidateIndexes.put(String.format(IndexTool.INDEX_JOB_NAME_TEMPLATE, - indexInfo.getTableSchem(), indexInfo.getDataTableName(), indexInfo.getTableName()), indexInfo); + Map<String, PhoenixAsyncIndex> candidateIndexes = new HashMap<>(); + try (Statement s = con.createStatement(); + ResultSet rs = s.executeQuery(CANDIDATE_INDEX_INFO_QUERY)) { + while (rs.next()) { + PhoenixAsyncIndex indexInfo = new PhoenixAsyncIndex(); + indexInfo.setIndexType(IndexType.fromSerializedValue(rs + .getByte(PhoenixDatabaseMetaData.INDEX_TYPE))); + indexInfo.setDataTableName(rs.getString(PhoenixDatabaseMetaData.DATA_TABLE_NAME)); + indexInfo.setTableSchem(rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM)); + indexInfo.setTableName(rs.getString(PhoenixDatabaseMetaData.TABLE_NAME)); + candidateIndexes.put(String.format(IndexTool.INDEX_JOB_NAME_TEMPLATE, + indexInfo.getTableSchem(), indexInfo.getDataTableName(), + indexInfo.getTableName()), indexInfo); + } } - return candidateIndexes; }