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
feature/appm-store/pbac
Yohan Avishke 5 years ago
parent 966a878e8e
commit 2e62acfafc

@ -527,7 +527,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public boolean deleteDevices(List<String> deviceIdentifiers, boolean transactionAlreadyExists) throws DeviceManagementException, public boolean deleteDevices(List<String> deviceIdentifiers) throws DeviceManagementException,
InvalidDeviceException { InvalidDeviceException {
if (deviceIdentifiers == null || deviceIdentifiers.isEmpty()) { if (deviceIdentifiers == null || deviceIdentifiers.isEmpty()) {
String msg = "Required values of device identifiers are not set to permanently delete device/s."; 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<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>(); List<DeviceCacheKey> deviceCacheKeyList = new ArrayList<>();
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
try { try {
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.openConnection();
List<Device> existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId); List<Device> existingDevices = deviceDAO.getDevicesByIdentifiers(deviceIdentifiers, tenantId);
DeviceManagementDAOFactory.closeConnection();
DeviceCacheKey deviceCacheKey; DeviceCacheKey deviceCacheKey;
for (Device device : existingDevices) { for (Device device : existingDevices) {
if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) { if (!EnrolmentInfo.Status.REMOVED.equals(device.getEnrolmentInfo().getStatus())) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType() String msg = "Device " + device.getDeviceIdentifier() + " of type " + device.getType()
+ " is not dis-enrolled to permanently delete the device"; + " is not dis-enrolled to permanently delete the device";
log.error(msg); log.error(msg);
@ -583,6 +583,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers); log.debug("Permanently deleting the details of devices : " + validDeviceIdentifiers);
} }
DeviceManagementDAOFactory.beginTransaction();
//deleting device from the core //deleting device from the core
deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds); deviceDAO.deleteDevices(validDeviceIdentifiers, new ArrayList<>(deviceIds), enrollmentIds);
for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) { for (Map.Entry<String, DeviceManager> entry : deviceManagerMap.entrySet()) {
@ -605,6 +606,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers); log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers);
} }
return true; 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) { } catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction"; String msg = "Error occurred while initiating transaction";
log.error(msg, e); log.error(msg, e);

Loading…
Cancel
Save