From 36e244297d759f66227bd6fd35da27eb0d1547ce Mon Sep 17 00:00:00 2001 From: Jave-Chen Date: Sat, 4 Jan 2020 10:38:15 +0800 Subject: [PATCH] Fix bug : Use try-with-resources or close this "ResultSet" in a "finally" clause (#1689) * #1688 Fix bug : Use try-with-resources or close this "ResultSet" in a "finally" clause * replace while to if where possible * merge TaskRecordDao.java from dev --- .../dao/utils/MysqlPerformance.java | 42 ++++++++++--------- .../dao/utils/PostgrePerformance.java | 39 ++++++++--------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/MysqlPerformance.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/MysqlPerformance.java index 019ef0cd72..40d12ab36f 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/MysqlPerformance.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/MysqlPerformance.java @@ -17,12 +17,7 @@ package org.apache.dolphinscheduler.dao.utils; -import org.apache.dolphinscheduler.common.enums.DbType; -import org.apache.dolphinscheduler.common.enums.Flag; -import org.apache.dolphinscheduler.dao.MonitorDBDao; -import org.apache.dolphinscheduler.dao.entity.MonitorRecord; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import static org.apache.dolphinscheduler.dao.MonitorDBDao.VARIABLE_NAME; import java.sql.Connection; import java.sql.ResultSet; @@ -30,7 +25,12 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Date; -import static org.apache.dolphinscheduler.dao.MonitorDBDao.VARIABLE_NAME; +import org.apache.dolphinscheduler.common.enums.DbType; +import org.apache.dolphinscheduler.common.enums.Flag; +import org.apache.dolphinscheduler.dao.MonitorDBDao; +import org.apache.dolphinscheduler.dao.entity.MonitorRecord; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * mysql performance @@ -55,26 +55,28 @@ public class MysqlPerformance extends BaseDBPerformance{ try{ pstmt = conn.createStatement(); - ResultSet rs1 = pstmt.executeQuery("show global variables"); - while(rs1.next()){ - if(rs1.getString(VARIABLE_NAME).toUpperCase().equals("MAX_CONNECTIONS")){ - monitorRecord.setMaxConnections( Long.parseLong(rs1.getString("value"))); + try (ResultSet rs1 = pstmt.executeQuery("show global variables")) { + while(rs1.next()){ + if(rs1.getString(VARIABLE_NAME).equalsIgnoreCase("MAX_CONNECTIONS")){ + monitorRecord.setMaxConnections( Long.parseLong(rs1.getString("value"))); + } } } - ResultSet rs2 = pstmt.executeQuery("show global status"); - while(rs2.next()){ - if(rs2.getString(VARIABLE_NAME).toUpperCase().equals("MAX_USED_CONNECTIONS")){ - monitorRecord.setMaxUsedConnections(Long.parseLong(rs2.getString("value"))); - }else if(rs2.getString(VARIABLE_NAME).toUpperCase().equals("THREADS_CONNECTED")){ - monitorRecord.setThreadsConnections(Long.parseLong(rs2.getString("value"))); - }else if(rs2.getString(VARIABLE_NAME).toUpperCase().equals("THREADS_RUNNING")){ - monitorRecord.setThreadsRunningConnections(Long.parseLong(rs2.getString("value"))); + try (ResultSet rs2 = pstmt.executeQuery("show global status")) { + while(rs2.next()){ + if(rs2.getString(VARIABLE_NAME).equalsIgnoreCase("MAX_USED_CONNECTIONS")){ + monitorRecord.setMaxUsedConnections(Long.parseLong(rs2.getString("value"))); + }else if(rs2.getString(VARIABLE_NAME).equalsIgnoreCase("THREADS_CONNECTED")){ + monitorRecord.setThreadsConnections(Long.parseLong(rs2.getString("value"))); + }else if(rs2.getString(VARIABLE_NAME).equalsIgnoreCase("THREADS_RUNNING")){ + monitorRecord.setThreadsRunningConnections(Long.parseLong(rs2.getString("value"))); + } } } }catch (Exception e) { monitorRecord.setState(Flag.NO); - logger.error("SQLException " + e); + logger.error("SQLException ", e); }finally { try { if (pstmt != null) { diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PostgrePerformance.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PostgrePerformance.java index d6471a9a9e..031fd00681 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PostgrePerformance.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/utils/PostgrePerformance.java @@ -16,6 +16,12 @@ */ package org.apache.dolphinscheduler.dao.utils; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Date; + import org.apache.dolphinscheduler.common.enums.DbType; import org.apache.dolphinscheduler.common.enums.Flag; import org.apache.dolphinscheduler.dao.MonitorDBDao; @@ -23,12 +29,6 @@ import org.apache.dolphinscheduler.dao.entity.MonitorRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Date; - /** * postgresql performance */ @@ -50,26 +50,27 @@ public class PostgrePerformance extends BaseDBPerformance { Statement pstmt= null; try{ pstmt = conn.createStatement(); - ResultSet rs1 = pstmt.executeQuery("select count(*) from pg_stat_activity;"); - while(rs1.next()){ - monitorRecord.setThreadsConnections(rs1.getInt("count")); - break; + + try (ResultSet rs1 = pstmt.executeQuery("select count(*) from pg_stat_activity;")) { + if(rs1.next()){ + monitorRecord.setThreadsConnections(rs1.getInt("count")); + } } - ResultSet rs2 = pstmt.executeQuery("show max_connections"); - while(rs2.next()){ - monitorRecord.setMaxConnections( rs2.getInt("max_connections")); - break; + try (ResultSet rs2 = pstmt.executeQuery("show max_connections")) { + if(rs2.next()){ + monitorRecord.setMaxConnections( rs2.getInt("max_connections")); + } } - ResultSet rs3 = pstmt.executeQuery("select count(*) from pg_stat_activity pg where pg.state = 'active';"); - while(rs3.next()){ - monitorRecord.setThreadsRunningConnections(rs3.getInt("count")); - break; + try (ResultSet rs3 = pstmt.executeQuery("select count(*) from pg_stat_activity pg where pg.state = 'active';")) { + if(rs3.next()){ + monitorRecord.setThreadsRunningConnections(rs3.getInt("count")); + } } }catch (Exception e) { monitorRecord.setState(Flag.NO); - logger.error("SQLException " + e); + logger.error("SQLException ", e); }finally { try { if (pstmt != null) {