Skip to content
Snippets Groups Projects
Commit 2fc8dae4 authored by Edileuton Henrique de Oliveira's avatar Edileuton Henrique de Oliveira
Browse files

webservice: preventing cannot get a connection error

parent 8601dce2
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment