From abc7f36067f48884d9830256e286114f92378d06 Mon Sep 17 00:00:00 2001 From: Ethan Lynn Date: Thu, 10 May 2018 17:32:19 +0800 Subject: Close db connections in finally block Fix bugs reported by sonar Change-Id: I0d3445856eb45533d904443b60d75c8aa03e5881 Issue-ID: SO-580 Signed-off-by: Ethan Lynn --- .../bpmn/plugin/urnmap/resources/URNResource.java | 33 +++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/bpmn/MSOURN-plugin/src/main/java/org/openecomp/camunda/bpmn/plugin/urnmap/resources/URNResource.java b/bpmn/MSOURN-plugin/src/main/java/org/openecomp/camunda/bpmn/plugin/urnmap/resources/URNResource.java index 1304fc24a7..85f9753634 100644 --- a/bpmn/MSOURN-plugin/src/main/java/org/openecomp/camunda/bpmn/plugin/urnmap/resources/URNResource.java +++ b/bpmn/MSOURN-plugin/src/main/java/org/openecomp/camunda/bpmn/plugin/urnmap/resources/URNResource.java @@ -60,14 +60,15 @@ public class URNResource extends AbstractCockpitPluginResource{ public List getUrnDataMap() { List list = new ArrayList(); - + PreparedStatement psData = null; + ResultSet r = null; try { conn = getDBConnection(); - PreparedStatement psData = conn + psData = conn .prepareStatement("select * from MSO_URN_MAPPING order by NAME_"); - ResultSet r = psData.executeQuery(); + r = psData.executeQuery(); while(r.next()) { @@ -78,14 +79,15 @@ public class URNResource extends AbstractCockpitPluginResource{ list.add(d); } - - psData.close(); - conn.close(); } catch (Exception e) { e.printStackTrace(); + } finally { + try { r.close(); } catch (Exception e) { /* ignored */ } + try { psData.close(); } catch (Exception e) { /* ignored */ } + try { conn.close(); } catch (Exception e) { /* ignored */ } } for(URNData d: list) @@ -158,24 +160,26 @@ public class URNResource extends AbstractCockpitPluginResource{ nRow.setVer_("1"); final String myKey = key_; final String myValue = value_; - + + PreparedStatement psData = null; msoLogger.debug("----------- START ----------------------"); try { conn = getDBConnection(); - PreparedStatement psData = conn + psData = conn .prepareStatement("Insert into MSO_URN_MAPPING values ('" + key_ + "', '" + value_ + "', '1')"); psData.executeUpdate(); - psData.close(); - conn.close(); //} } catch (Exception e) { e.printStackTrace(); + } finally { + try { psData.close(); } catch (Exception e) { /* ignored */ } + try { conn.close(); } catch (Exception e) { /* ignored */ } } // getQueryService().executeQuery("cockpit.urnMap.insertNewRow", nRow, URNData.class); } @@ -184,21 +188,22 @@ public class URNResource extends AbstractCockpitPluginResource{ public void getPersistData(URNData d) { //getQueryService().executeQuery("cockpit.urnMap.persistURNData", d, URNData.class); - + PreparedStatement psData = null; try { conn = getDBConnection(); - PreparedStatement psData = conn + psData = conn .prepareStatement("UPDATE MSO_URN_MAPPING set VALUE_ ='"+ d.getURNValue() + "' WHERE NAME_='" + d.getURNName() + "'"); psData.executeUpdate(); - psData.close(); - conn.close(); } catch (Exception e) { e.printStackTrace(); + } finally { + try { psData.close(); } catch (Exception e) { /* ignored */ } + try { conn.close(); } catch (Exception e) { /* ignored */ } } } -- cgit 1.2.3-korg