fix for sonar blocker in DailyLatencyReport.Java 19/63819/1
authorAmaresh Kumar <kamaresh@in.ibm.com>
Thu, 30 Aug 2018 13:31:35 +0000 (19:01 +0530)
committerAmaresh Kumar <kamaresh@in.ibm.com>
Thu, 30 Aug 2018 13:33:09 +0000 (19:03 +0530)
Fixed 3 Sonar issues.

https://sonar.onap.org/project/issues?fileUuids=AWHFaQitEU5FCDfKtnEP&id=org.onap.dmaap.datarouter%3Aparent&resolved=false&severities=BLOCKER

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

index a5281c0..28740c0 100644 (file)
@@ -152,41 +152,40 @@ public class DailyLatencyReport extends ReportBase {
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
-            ps.setLong(1, from);\r
-            ps.setLong(2, to);\r
-            ResultSet rs = ps.executeQuery();\r
-            while (rs.next()) {\r
-                String id   = rs.getString("PUBLISH_ID");\r
-                int feed    = rs.getInt("FEEDID");\r
-                long etime  = rs.getLong("EVENT_TIME");\r
-                String type = rs.getString("TYPE");\r
-                String fid  = rs.getString("FEED_FILEID");\r
-                long clen   = rs.getLong("CONTENT_LENGTH");\r
-                String date = sdf.format(new Date(getPstart(id)));\r
-                String key  = date + "," + feed;\r
-                Counters c = map.get(key);\r
-                if (c == null) {\r
-                    c = new Counters(date, feed);\r
-                    map.put(key, c);\r
+            try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
+                ps.setLong(1, from);\r
+                ps.setLong(2, to);\r
+                try(ResultSet rs = ps.executeQuery()) {\r
+                    while (rs.next()) {\r
+                        String id = rs.getString("PUBLISH_ID");\r
+                        int feed = rs.getInt("FEEDID");\r
+                        long etime = rs.getLong("EVENT_TIME");\r
+                        String type = rs.getString("TYPE");\r
+                        String fid = rs.getString("FEED_FILEID");\r
+                        long clen = rs.getLong("CONTENT_LENGTH");\r
+                        String date = sdf.format(new Date(getPstart(id)));\r
+                        String key = date + "," + feed;\r
+                        Counters c = map.get(key);\r
+                        if (c == null) {\r
+                            c = new Counters(date, feed);\r
+                            map.put(key, c);\r
+                        }\r
+                        c.addEvent(etime, type, id, fid, clen);\r
+                    }\r
                 }\r
-                c.addEvent(etime, type, id, fid, clen);\r
+\r
+                db.release(conn);\r
             }\r
-            rs.close();\r
-            ps.close();\r
-            db.release(conn);\r
         } catch (SQLException e) {\r
             e.printStackTrace();\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis()-start) + " ms");\r
-        try {\r
-            PrintWriter os = new PrintWriter(outfile);\r
+        try (PrintWriter os = new PrintWriter(outfile)){\r
             os.println("date,feedid,minsize,maxsize,avgsize,minlat,maxlat,avglat,fanout");\r
             for (String key : new TreeSet<String>(map.keySet())) {\r
                 Counters c = map.get(key);\r
                 os.println(c.toString());\r
             }\r
-            os.close();\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: "+outfile);\r
         }\r