Use try with resources for dao layer

Pasindu Rupasinghe 2 months ago
parent 6d324c084a
commit 87b98a6cb5

@ -130,11 +130,8 @@ public class GenericOperationDAOImpl implements OperationDAO {
public int updateOperationByDeviceTypeAndInitialStatus(String deiceType, String initialStatus, String requiredStatus) public int updateOperationByDeviceTypeAndInitialStatus(String deiceType, String initialStatus, String requiredStatus)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null;
int numOfRecordsUpdated; int numOfRecordsUpdated;
try {
long time = DeviceManagementDAOUtil.getCurrentUTCTime(); long time = DeviceManagementDAOUtil.getCurrentUTCTime();
Connection connection = OperationManagementDAOFactory.getConnection();
String sql = "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS=?, UPDATED_TIMESTAMP=? WHERE DEVICE_TYPE=?"; String sql = "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS=?, UPDATED_TIMESTAMP=? WHERE DEVICE_TYPE=?";
@ -144,7 +141,10 @@ public class GenericOperationDAOImpl implements OperationDAO {
sql += " AND STATUS=?"; sql += " AND STATUS=?";
} }
stmt = connection.prepareStatement(sql); try (
Connection connection = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql)
) {
stmt.setString(1, requiredStatus); stmt.setString(1, requiredStatus);
stmt.setLong(2, time); stmt.setLong(2, time);
stmt.setString(3, deiceType); stmt.setString(3, deiceType);
@ -156,8 +156,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + throw new OperationManagementDAOException("Error occurred while update device mapping operation status " +
e.getMessage(), e); e.getMessage(), e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
} }
return numOfRecordsUpdated; return numOfRecordsUpdated;
} }

Loading…
Cancel
Save