Fixed Sonar Issues in LatencyReport.Java 99/63799/1
authorAmaresh Kumar <kamaresh@in.ibm.com>
Thu, 30 Aug 2018 12:15:54 +0000 (17:45 +0530)
committerAmaresh Kumar <kamaresh@in.ibm.com>
Thu, 30 Aug 2018 12:23:34 +0000 (17:53 +0530)
Fixed 3 Sonar issues.

Issue-ID: DMAAP-676
Change-Id: I5f1862843a10eae8f9ba7f0d04a635c5f95b807e
Signed-off-by: Amaresh Kumar <kamaresh@in.ibm.com>
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/LatencyReport.java

index ba8f15a..549511b 100644 (file)
@@ -145,40 +145,38 @@ public class LatencyReport extends ReportBase {
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
+            try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)){\r
             ps.setLong(1, from);\r
             ps.setLong(2, to);\r
-            ResultSet rs = ps.executeQuery();\r
-            PrintWriter os = new PrintWriter(outfile);\r
-            os.println("recordid,feedid,uri,size,min,max,avg,fanout");\r
-            Counters c = null;\r
-            while (rs.next()) {\r
-                long etime = rs.getLong("EVENT_TIME");\r
-                String type = rs.getString("TYPE");\r
-                String id = rs.getString("PUBLISH_ID");\r
-                String fid = rs.getString("FEED_FILEID");\r
-                int feed = rs.getInt("FEEDID");\r
-                long clen = rs.getLong("CONTENT_LENGTH");\r
-                if (c != null && !id.equals(c.id)) {\r
-                    String line = id + "," + c.toString();\r
-                    os.println(line);\r
-                    c = null;\r
+            try(ResultSet rs = ps.executeQuery()) {\r
+                try(PrintWriter os = new PrintWriter(outfile)) {\r
+                    os.println("recordid,feedid,uri,size,min,max,avg,fanout");\r
+                    Counters c = null;\r
+                    while (rs.next()) {\r
+                        long etime = rs.getLong("EVENT_TIME");\r
+                        String type = rs.getString("TYPE");\r
+                        String id = rs.getString("PUBLISH_ID");\r
+                        String fid = rs.getString("FEED_FILEID");\r
+                        int feed = rs.getInt("FEEDID");\r
+                        long clen = rs.getLong("CONTENT_LENGTH");\r
+                        if (c != null && !id.equals(c.id)) {\r
+                            String line = id + "," + c.toString();\r
+                            os.println(line);\r
+                            c = null;\r
+                        }\r
+                        if (c == null) {\r
+                            c = new Counters(id, feed, clen, fid);\r
+                        }\r
+                        if (feed != c.feedid)\r
+                            System.err.println("Feed ID mismatch, " + feed + " <=> " + c.feedid);\r
+                        if (clen != c.clen)\r
+                            System.err.println("Cont Len mismatch, " + clen + " <=> " + c.clen);\r
+                        c.addEvent(type, etime);\r
+                    }\r
                 }\r
-                if (c == null) {\r
-                    c = new Counters(id, feed, clen, fid);\r
-                }\r
-                if (feed != c.feedid)\r
-                    System.err.println("Feed ID mismatch, " + feed + " <=> " + c.feedid);\r
-                if (clen != c.clen)\r
-                    System.err.println("Cont Len mismatch, " + clen + " <=> " + c.clen);\r
-//                if (fid != c.fileid)\r
-//                    System.err.println("File ID mismatch, "+fid+" <=> "+c.fileid);\r
-                c.addEvent(type, etime);\r
+             db.release(conn);\r
+            }\r
             }\r
-            rs.close();\r
-            ps.close();\r
-            db.release(conn);\r
-            os.close();\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r
         } catch (SQLException e) {\r