Fix sonar issues in DbUtils.java 35/72035/4
authorParshad Patel <pars.patel@samsung.com>
Wed, 7 Nov 2018 04:32:40 +0000 (13:32 +0900)
committerParshad Patel <pars.patel@samsung.com>
Wed, 7 Nov 2018 08:07:40 +0000 (17:07 +0900)
Fix use try with resources issue

Issue-ID: PORTAL-342
Change-Id: Ifb5b4b39091ff09349a88ad7db9c7956710c6b3f
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/DbUtils.java

index 1ce53c7..e179d38 100644 (file)
@@ -66,17 +66,18 @@ public class DbUtils {
 
        public static Connection getConnection() throws ReportSQLException {
                try {
-               return AppUtils.getDatasource().getConnection();
+                   return AppUtils.getDatasource().getConnection();
                } catch(SQLException ex) {
-                       ex.printStackTrace();
+                   ex.printStackTrace();
                }
                return null;
        } // getConnection
 
        public static void clearConnection(Connection con) throws ReportSQLException {
                try {
-        if ((con != null) && !con.isClosed()) 
-                 Globals.getDbUtils().clearConnection(con);
+               if ((con != null) && !con.isClosed()) {
+                   Globals.getDbUtils().clearConnection(con);
+               }
                } catch (SQLException ex) {
                        throw new ReportSQLException(ex.getMessage(), ex.getCause());
                } catch (Exception ex2 ) {
@@ -123,16 +124,16 @@ public class DbUtils {
                String result = null;
 
                try {
-            if(con.isClosed()) con = getConnection();            
+            if(con.isClosed()) con = getConnection();
             logger.debug(EELFLoggerDelegate.debugLogger, ("[SQL CALL FROM RAPTOR] [SQL Call] " + sql));
-                       CallableStatement stmt = con.prepareCall(sql);
-                       if (expectResult)
-                               stmt.registerOutParameter(1, Types.CHAR);
-                       stmt.executeUpdate();
-                       if (expectResult)
-                               result = stmt.getString(1);
-                       stmt.close();
-            con.commit();
+                       try(CallableStatement stmt = con.prepareCall(sql)){
+                       if (expectResult)
+                               stmt.registerOutParameter(1, Types.CHAR);
+                       stmt.executeUpdate();
+                       if (expectResult)
+                               result = stmt.getString(1);
+                con.commit();
+                       }
                } catch (SQLException e) {
                        throw new ReportSQLException(e.getMessage(), sql);
                } finally {
@@ -152,20 +153,18 @@ public class DbUtils {
        } // executeCall
 
        public static int executeUpdate(Connection con, String sql) throws ReportSQLException {
-        int rcode = -1;        
-               try {
-                       Statement stmt = con.createStatement();
+        int rcode = -1;
+               try(Statement stmt = con.createStatement()) {
                        logger.debug(EELFLoggerDelegate.debugLogger, ("[SQL CALL FROM RAPTOR] [SQL Update] " + sql));
                        rcode = stmt.executeUpdate(sql);
-                   stmt.close();
-            //con.commit();            
+            //con.commit();
                } catch (SQLException e) {
             //e.printStackTrace();
                        throw new ReportSQLException(e.getMessage(), sql);
-               } 
-        return rcode;        
+               }
+        return rcode;
        } // executeUpdate
-    
+
     public static int executeUpdate(String sql) throws ReportSQLException {
                Connection con = null;
                try {
@@ -189,23 +188,17 @@ public class DbUtils {
        public static DataSet executeQuery(Connection con, String sql, int maxRowLimit)
                        throws ReportSQLException {
                try {
-            if(con.isClosed()) con = getConnection(); 
+            if(con.isClosed()) con = getConnection();
             //con.
-                       Statement stmt = con.createStatement();
-
-                       logger.debug(EELFLoggerDelegate.debugLogger, ("[SQL CALL FROM RAPTOR] [SQL] " + sql));
-                       ResultSet rs = stmt.executeQuery(sql);
-                       DataSet ds = new DataSet(rs, maxRowLimit);
-
-                          if(rs!=null) 
-                                       rs.close();
-                          if(stmt!=null)
-                               stmt.close();
-
-                       return ds;
+                       try(Statement stmt = con.createStatement();
+                               ResultSet rs = stmt.executeQuery(sql))
+                       {
+                           logger.debug(EELFLoggerDelegate.debugLogger, ("[SQL CALL FROM RAPTOR] [SQL] " + sql));
+                           return new DataSet(rs, maxRowLimit);
+                       }
                } catch (SQLException e) {
                        throw new ReportSQLException(e.getMessage(), sql);
-               } 
+               }
        } // executeQuery
 
        public static DataSet executeQuery(String sql) throws ReportSQLException  {