From 84c33621751a4f70ca72da23dd71ddeb7c1f9915 Mon Sep 17 00:00:00 2001 From: Amaresh Kumar Date: Tue, 4 Sep 2018 18:21:31 +0530 Subject: Fixed Sonar Issues in EgressRoute.java https://sonar.onap.org/project/issues?id=org.onap.dmaap.datarouter%3Aparent&open=AWHFaQqiEU5FCDfKtnge&resolved=false&severities=BLOCKER&types=BUG Issue-ID: DMAAP-696 Change-Id: If5fad1a181bf77bc2f9aa231e7e81f72db5363e2 Signed-off-by: Amaresh Kumar --- .../datarouter/provisioning/beans/EgressRoute.java | 51 ++++++++++++++-------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java index fa23fa1c..c6788e08 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRoute.java @@ -29,6 +29,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Objects; import java.util.SortedSet; import java.util.TreeSet; @@ -59,15 +60,16 @@ public class EgressRoute extends NodeClass implements Comparable { DB db = new DB(); @SuppressWarnings("resource") Connection conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES"); - while (rs.next()) { - int subid = rs.getInt("SUBID"); - int nodeid = rs.getInt("NODEID"); - set.add(new EgressRoute(subid, nodeid)); - } - rs.close(); - stmt.close(); + try( Statement stmt = conn.createStatement()) { + try(ResultSet rs = stmt.executeQuery("select SUBID, NODEID from EGRESS_ROUTES")) { + while (rs.next()) { + int subid = rs.getInt("SUBID"); + int nodeid = rs.getInt("NODEID"); + set.add(new EgressRoute(subid, nodeid)); + } + } + } + db.release(conn); } catch (SQLException e) { e.printStackTrace(); @@ -91,19 +93,21 @@ public class EgressRoute extends NodeClass implements Comparable { String sql = "select NODEID from EGRESS_ROUTES where SUBID = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, sub); - ResultSet rs = ps.executeQuery(); - if (rs.next()) { - int node = rs.getInt("NODEID"); - v = new EgressRoute(sub, node); + try(ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + int node = rs.getInt("NODEID"); + v = new EgressRoute(sub, node); + } } - rs.close(); ps.close(); db.release(conn); } catch (SQLException e) { e.printStackTrace(); } finally { try { - ps.close(); + if(ps!=null) { + ps.close(); + } } catch (SQLException e) { e.printStackTrace(); } @@ -139,7 +143,9 @@ public class EgressRoute extends NodeClass implements Comparable { e.printStackTrace(); } finally { try { - ps.close(); + if(ps!=null) { + ps.close(); + } } catch (SQLException e) { e.printStackTrace(); } @@ -165,7 +171,9 @@ public class EgressRoute extends NodeClass implements Comparable { e.printStackTrace(); } finally { try { - ps.close(); + if(ps!=null) { + ps.close(); + } } catch (SQLException e) { e.printStackTrace(); } @@ -189,7 +197,9 @@ public class EgressRoute extends NodeClass implements Comparable { e.printStackTrace(); } finally { try { - ps.close(); + if(ps!=null) { + ps.close(); + } } catch (SQLException e) { e.printStackTrace(); } @@ -226,4 +236,9 @@ public class EgressRoute extends NodeClass implements Comparable { public String toString() { return String.format("EGRESS: sub=%d, node=%d", subid, nodeid); } + + @Override + public int hashCode() { + return Objects.hash(subid, nodeid); + } } -- cgit 1.2.3-korg