diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 7b233b90ca2..75f3836a349 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1147,7 +1147,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List deviceStatus; for (Device device : allDevices) { long dateDiff = 0; - deviceStatus = getDeviceStatusHistory(device, null, endDate, true); + deviceStatus = getDeviceStatusHistoryInsideTransaction(device, null, endDate, true); if (device.getEnrolmentInfo().getDateOfEnrolment() < startDate.getTime()) { if (!deviceStatus.isEmpty() && (String.valueOf(deviceStatus.get(0).getStatus()).equals("REMOVED") || String.valueOf(deviceStatus.get(0).getStatus()).equals("DELETED"))) { @@ -2202,23 +2202,52 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } - @Override - public List getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException { + /* + This is just to avoid breaking the billing functionality as it required to call getDeviceStatusHistory method + without transaction handling. + */ + private List getDeviceStatusHistoryInsideTransaction( + Device device, Date fromDate, Date toDate, boolean billingStatus) + throws DeviceManagementException { if (log.isDebugEnabled()) { log.debug("get status history of device: " + device.getDeviceIdentifier()); } try { + DeviceManagementDAOFactory.getConnection(); int tenantId = this.getTenantId(); return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus); } catch (DeviceManagementDAOException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while retrieving status history"; + String msg = "Error occurred in retrieving status history for device :" + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); - } catch (Exception e) { + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.info(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public List getDeviceStatusHistory(Device device, Date fromDate, Date toDate, boolean billingStatus) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("get status history of device: " + device.getDeviceIdentifier()); + } + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = this.getTenantId(); + return deviceStatusDAO.getStatus(device.getId(), tenantId, fromDate, toDate, billingStatus); + } catch (DeviceManagementDAOException e) { String msg = "Error occurred in retrieving status history for device :" + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.info(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } }