From 78ad4ce883b8685f6248d4da832faa1c400666ff Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Wed, 7 Nov 2018 13:32:40 +0900 Subject: Fix sonar issues in DbUtils.java Fix use try with resources issue Issue-ID: PORTAL-342 Change-Id: Ifb5b4b39091ff09349a88ad7db9c7956710c6b3f Signed-off-by: Parshad Patel --- .../onap/portalsdk/analytics/system/DbUtils.java | 63 ++++++++++------------ 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/DbUtils.java b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/DbUtils.java index 1ce53c71..e179d388 100644 --- a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/DbUtils.java +++ b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/DbUtils.java @@ -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 { -- cgit 1.2.3-korg