Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / reports / LatencyReport.java
index f500140..f98116d 100644 (file)
@@ -32,8 +32,7 @@ import java.sql.ResultSet;
 import java.sql.SQLException;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
-\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
 \r
 /**\r
  * Generate a per-file latency report.  It reports on the details related to one file published\r
@@ -54,9 +53,6 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB;
  * @version $Id: LatencyReport.java,v 1.1 2013/10/28 18:06:53 eby Exp $\r
  */\r
 public class LatencyReport extends ReportBase {\r
-    private static final String SELECT_SQL =\r
-            "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS" +\r
-                    " where EVENT_TIME >= ? and EVENT_TIME <= ? order by PUBLISH_ID, EVENT_TIME";\r
 \r
     private class Event {\r
         public final String type;\r
@@ -80,7 +76,7 @@ public class LatencyReport extends ReportBase {
             feedid = fid;\r
             clen = c;\r
             fileid = s;\r
-            events = new ArrayList<Event>();\r
+            events = new ArrayList<>();\r
         }\r
 \r
         private long pubtime;\r
@@ -141,15 +137,14 @@ public class LatencyReport extends ReportBase {
     @Override\r
     public void run() {\r
         long start = System.currentTimeMillis();\r
-        try {\r
-            DB db = new DB();\r
-            @SuppressWarnings("resource")\r
-            Connection conn = db.getConnection();\r
-            try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)){\r
+        try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+            PreparedStatement ps = conn.prepareStatement(\r
+                "select EVENT_TIME, TYPE, PUBLISH_ID, FEED_FILEID, FEEDID, CONTENT_LENGTH from LOG_RECORDS where "\r
+                    + "EVENT_TIME >= ? and EVENT_TIME <= ? order by PUBLISH_ID, EVENT_TIME")) {\r
             ps.setLong(1, from);\r
             ps.setLong(2, to);\r
             try(ResultSet rs = ps.executeQuery()) {\r
-                try(PrintWriter os = new PrintWriter(outfile)) {\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
@@ -174,8 +169,6 @@ public class LatencyReport extends ReportBase {
                         c.addEvent(type, etime);\r
                     }\r
                 }\r
-             db.release(conn);\r
-            }\r
             }\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r