diff --git a/webservice/DataSID.java b/webservice/DataSID.java index 1bf05306191d7edef6e17f966343cfb434bd9444..e6af44f20c68ec85abec2defb3492ca6794604b8 100644 --- a/webservice/DataSID.java +++ b/webservice/DataSID.java @@ -199,16 +199,14 @@ public class DataSID { * @return String */ public String setInventory(String xmlData) { + Connection con = null; + String status = "Success"; try { InitialContext cxt = new InitialContext(); DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/simmc"); if (ds == null) throw new Exception("Data source not found!"); - Connection con = ds.getConnection(); - if (con == null) - throw new Exception("Failed to get a database connection!"); - SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = factory.newSchema(this.xml_inventory_schema); @@ -229,6 +227,10 @@ public class DataSID { Calendar cal = Calendar.getInstance(); java.sql.Date contactDate = new java.sql.Date(cal.getTimeInMillis()); + con = ds.getConnection(); + if (con == null) + throw new Exception("Failed to get a database connection!"); + PreparedStatement st = createInventoryStatement(con, collected, contactDate); st.executeUpdate(); @@ -244,16 +246,24 @@ public class DataSID { macaddr, user.getName(), user.getLogin(), user.getLogout()); st.executeUpdate(); } - con.close(); - log(INFO, "setInventory(idpoint=" + collected.getPointInfo().getIdpoint() + ", macaddr=" + interfaces.get(0).getMacAddress() + ")"); - return "Success"; } catch (Exception e) { log(ERROR, e.getMessage() + " " + xmlData); e.printStackTrace(); - return "ERROR: " + e.getMessage(); + status = "ERROR: " + e.getMessage(); + } finally { + try{ + if (con != null) + con.close(); + } catch (SQLException se) { + log(ERROR, se.getMessage()); + se.printStackTrace(); + return "ERROR: " + se.getMessage(); + } + + return status; } } @@ -409,16 +419,14 @@ public class DataSID { */ public String setNetUsage(String xmlData) { + Connection con = null; + String status = "Success"; try { InitialContext cxt = new InitialContext(); DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/simmc"); if (ds == null) throw new Exception("Data source not found!"); - Connection con = ds.getConnection(); - if (con == null) - throw new Exception("Failed to get a database connection!"); - SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = factory.newSchema(this.xml_net_usage_schema); @@ -448,6 +456,10 @@ public class DataSID { List<NetUse> netUses = netCollectedData.getBandwidthUsage().getNetuse(); + con = ds.getConnection(); + if (con == null) + throw new Exception("Failed to get a database connection!"); + for(NetUse netUse : netUses) { PreparedStatement st = createNetUsageStatement(con, contactDate, netCollectedData.getIdPoint(), (Object)macaddr, @@ -456,15 +468,25 @@ public class DataSID { netUse.getTx().getTxPackets()); st.executeUpdate(); } - con.close(); log(DEBUG, "setNetUsage(idpoint=" + netCollectedData.getIdPoint() + ", macaddr=" + interfaces.get(0).getMacAddress() + ")"); - return "Success"; + status = "Success"; } catch (Exception e) { log(ERROR, e.getMessage() + " " + xmlData); e.printStackTrace(); - return "ERROR: " + e.getMessage(); + status = "ERROR: " + e.getMessage(); + } finally { + try{ + if (con != null) + con.close(); + } catch (SQLException se) { + log(ERROR, se.getMessage()); + se.printStackTrace(); + return "ERROR: " + se.getMessage(); + } + + return status; } }