From 2e62acfafc5a7c533e1b9f6e56bd939558497025 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 18 Dec 2019 14:02:18 +0530 Subject: [PATCH] Fix issue with deleting devices Already dis-enrolled devices won't be deleted due to attempting to create a new DAO connection while a one already exists for the same DAO Factory. This error will occur when starting a server with already dis-enrolled devices and by trying to delete them --- .../service/DeviceManagementProviderServiceImpl.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 0c65a79fa8..a823a76bfb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -527,7 +527,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean deleteDevices(List deviceIdentifiers, boolean transactionAlreadyExists) throws DeviceManagementException, + public boolean deleteDevices(List deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException { if (deviceIdentifiers == null || deviceIdentifiers.isEmpty()) { String msg = "Required values of device identifiers are not set to permanently delete device/s."; @@ -542,12 +542,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List deviceCacheKeyList = new ArrayList<>(); int tenantId = this.getTenantId(); try { - DeviceManagementDAOFactory.beginTransaction(); + DeviceManagementDAOFactory.openConnection(); List existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId); + DeviceManagementDAOFactory.closeConnection(); DeviceCacheKey deviceCacheKey; for (Device device : existingDevices) { if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) { - DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType() + " is not dis-enrolled to permanently delete the device"; log.error(msg); @@ -583,6 +583,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (log.isDebugEnabled()) { log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers); } + DeviceManagementDAOFactory.beginTransaction(); //deleting device from the core deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); for (Map.Entry entry : deviceManagerMap.entrySet()) { @@ -605,6 +606,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers); } return true; + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction"; log.error(msg, e);