diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationMSSQLDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationMSSQLDAOImpl.java index 27a63ec251..511be28cfc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationMSSQLDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationMSSQLDAOImpl.java @@ -49,32 +49,31 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; try { operation.setCreatedTimeStamp(new Timestamp(new Date().getTime()).toString()); Connection connection = OperationManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " + "INITIATED_BY, OPERATION_DETAILS) VALUES (?, ?, ?, ?, ?, ?)"; - stmt = connection.prepareStatement(sql, new String[]{"id"}); - stmt.setString(1, operation.getType().toString()); - stmt.setLong(2, DeviceManagementDAOUtil.getCurrentUTCTime()); - stmt.setLong(3, 0); - stmt.setString(4, operation.getCode()); - stmt.setString(5, operation.getInitiatedBy()); - stmt.setObject(6, operation); - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - int id = -1; - if (rs.next()) { - id = rs.getInt(1); + try (PreparedStatement stmt = connection.prepareStatement(sql, new String[]{"id"})) { + stmt.setString(1, operation.getType().toString()); + stmt.setLong(2, DeviceManagementDAOUtil.getCurrentUTCTime()); + stmt.setLong(3, 0); + stmt.setString(4, operation.getCode()); + stmt.setString(5, operation.getInitiatedBy()); + stmt.setObject(6, operation); + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + int id = -1; + if (rs.next()) { + id = rs.getInt(1); + } + return id; + } } - return id; } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while adding command operation", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while adding command operation" + e; + log.error(msg); + throw new OperationManagementDAOException(msg); } } @@ -122,39 +121,41 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl { @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; ConfigOperation configOperation = null; - ByteArrayInputStream bais; ObjectInputStream ois; try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT ID, ENABLED, OPERATION_DETAILS FROM DM_OPERATION WHERE ID = ? AND TYPE='CONFIG'"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, operationId); - rs = stmt.executeQuery(); - - if (rs.next()) { - byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); - bais = new ByteArrayInputStream(operationDetails); - ois = new ObjectInputStream(bais); - configOperation = (ConfigOperation) ois.readObject(); - configOperation.setId(rs.getInt("ID")); - configOperation.setEnabled(rs.getBoolean("ENABLED")); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, operationId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + configOperation = (ConfigOperation) ois.readObject(); + configOperation.setId(rs.getInt("ID")); + configOperation.setEnabled(rs.getBoolean("ENABLED")); + } + } } } catch (IOException e) { - throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " + - "object", e); + String msg = "IO Error occurred while de serialize the policy operation " + + "object" + e; + log.error(msg); + throw new OperationManagementDAOException(msg); } catch (ClassNotFoundException e) { - throw new OperationManagementDAOException("Class not found error occurred while de serialize the policy " + - "operation object", e); + String msg = "Class not found error occurred while de serialize the policy " + + "operation object" + e; + log.error(msg); + throw new OperationManagementDAOException(msg); } catch (SQLException e) { - throw new OperationManagementDAOException("SQL Error occurred while retrieving the policy operation " + + String msg = "SQL Error occurred while retrieving the policy operation " + "object available for the id '" - + operationId, e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); + + operationId; + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } return configOperation; } @@ -162,11 +163,8 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl { @Override public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; ConfigOperation configOperation; List operations = new ArrayList<>(); - ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { @@ -174,30 +172,36 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl { String sql = "SELECT co.ID, co.OPERATION_DETAILS FROM DM_OPERATION co " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? " + "AND STATUS = ?) dm ON dm.OPERATION_ID = co.ID WHERE co.TYPE = 'CONFIG'"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, enrolmentId); - stmt.setString(2, status.toString()); - rs = stmt.executeQuery(); - - while (rs.next()) { - byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); - bais = new ByteArrayInputStream(operationDetails); - ois = new ObjectInputStream(bais); - configOperation = (ConfigOperation) ois.readObject(); - configOperation.setStatus(status); - configOperation.setId(rs.getInt("ID")); - operations.add(configOperation); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, enrolmentId); + stmt.setString(2, status.toString()); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + configOperation = (ConfigOperation) ois.readObject(); + configOperation.setStatus(status); + configOperation.setId(rs.getInt("ID")); + operations.add(configOperation); + } + } } } catch (IOException e) { - throw new OperationManagementDAOException("IO Error occurred while de serialize the configuration " + - "operation object", e); + String msg = "IO Error occurred while de serialize the configuration " + + "operation object" + e; + log.error(msg); + throw new OperationManagementDAOException(msg); } catch (ClassNotFoundException e) { - throw new OperationManagementDAOException("Class not found error occurred while de serialize the " + - "configuration operation object", e); + String msg = "Class not found error occurred while de serialize the " + + "configuration operation object" + e; + log.error(msg); + throw new OperationManagementDAOException(msg); } catch (SQLException e) { - throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " + - "for the device'" + enrolmentId + "' with status '" + status.toString(), e); + String msg = "SQL error occurred while retrieving the operation available " + + "for the device'" + enrolmentId + "' with status '" + status.toString(); + log.error(msg, e); + throw new OperationManagementDAOException(msg, e); } finally { if (bais != null) { try { @@ -213,7 +217,6 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl { log.warn("Error occurred while closing ObjectOutputStream", e); } } - OperationManagementDAOUtil.cleanupResources(stmt, rs); } return operations; }