From: Amaresh Kumar Date: Wed, 29 Aug 2018 15:11:19 +0000 (+0530) Subject: Fixed Sonar Blocker in StatisticsServlet X-Git-Tag: 1.0.1~42^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=commitdiff_plain;h=c3e6bf5bde573fe733f5b5860610d3df73630014 Fixed Sonar Blocker in StatisticsServlet Fixed 4 Blocker issues in StatisticsServlet.java https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open= AWHFaQmXEU5FCDfKtnMB&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open= AWHFaQmXEU5FCDfKtnL-&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open= AWHFaQmXEU5FCDfKtnL_&resolved=false&severities=BLOCKER&types=BUG https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open =AWHFaQmXEU5FCDfKtnMA&resolved=false&severities=BLOCKER&types=BUG Issue-ID: DMAAP-674 Change-Id: I3dbc50523e0fda002c0a26fa05b23814bf8563bc Signed-off-by: Amaresh Kumar --- diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java index 134a595b..e79d4705 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java @@ -293,13 +293,13 @@ public class StatisticsServlet extends BaseServlet { /** * getFeedIdsByGroupId - Getting FEEDID's by GROUP ID. * - * @throws SQL Query SQLException. + * @throws SQLException Query SQLException. */ public StringBuffer getFeedIdsByGroupId(int groupIds) throws SQLException { DB db = null; Connection conn = null; - PreparedStatement prepareStatement = null; + //PreparedStatement prepareStatement = null; ResultSet resultSet = null; String sqlGoupid = null; StringBuffer feedIds = new StringBuffer(); @@ -308,16 +308,16 @@ public class StatisticsServlet extends BaseServlet { db = new DB(); conn = db.getConnection(); sqlGoupid = " SELECT FEEDID from FEEDS WHERE GROUPID = ?"; - prepareStatement = conn.prepareStatement(sqlGoupid); - prepareStatement.setInt(1, groupIds); - resultSet = prepareStatement.executeQuery(); - while (resultSet.next()) { - feedIds.append(resultSet.getInt("FEEDID")); - feedIds.append(","); + try(PreparedStatement prepareStatement = conn.prepareStatement(sqlGoupid)) { + prepareStatement.setInt(1, groupIds); + resultSet = prepareStatement.executeQuery(); + while (resultSet.next()) { + feedIds.append(resultSet.getInt("FEEDID")); + feedIds.append(","); + } + feedIds.deleteCharAt(feedIds.length() - 1); + System.out.println("feedIds" + feedIds.toString()); } - feedIds.deleteCharAt(feedIds.length() - 1); - System.out.println("feedIds" + feedIds.toString()); - } catch (SQLException e) { e.printStackTrace(); } finally { @@ -326,13 +326,7 @@ public class StatisticsServlet extends BaseServlet { resultSet.close(); resultSet = null; } - - if (prepareStatement != null) { - prepareStatement.close(); - prepareStatement = null; - } - - if (conn != null) { + if (conn != null) { db.release(conn); } } catch (Exception e) { @@ -571,33 +565,44 @@ public class StatisticsServlet extends BaseServlet { intlogger.info("Error parsing time=" + s); return -1; } - - private ResultSet getRecordsForSQL(String sql) { - intlogger.debug(sql); - long start = System.currentTimeMillis(); - DB db = new DB(); - Connection conn = null; - ResultSet rs = null; - - try { - conn = db.getConnection(); - Statement stmt = conn.createStatement(); - PreparedStatement pst = conn.prepareStatement(sql); - rs = pst.executeQuery(); - //this.rsToJson(rs) - //rs.close(); - stmt.close(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - if (conn != null) { - db.release(conn); - } - } + intlogger.debug(sql); + long start = System.currentTimeMillis(); + DB db = new DB(); + Connection conn = null; + ResultSet rs = null; + Statement stmt = null; + PreparedStatement pst = null; + try { + conn = db.getConnection(); + stmt = conn.createStatement(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + + try { + if (conn != null) { + db.release(conn); + } + if (stmt != null) { + stmt.close(); + } + if (pst != null) { + pst.close(); + } + if (rs != null) { + rs.close(); + } + + } catch (SQLException sqlException) { + intlogger.error("Exception in getting SQl Records",sqlException); + } - intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); + intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); - return rs; + return rs; + } } }