Fixing connection leaks spotted in operation management implementation

revert-70aa11f8
prabathabey 10 years ago
parent ff2c29a158
commit 87e632866b

@ -75,7 +75,7 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public int addOperation(Operation operation, public int addOperation(Operation operation,
List<DeviceIdentifier> deviceIds) throws OperationManagementException { List<DeviceIdentifier> deviceIds) throws OperationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("operation:[" + operation.toString() + "]"); log.debug("operation:[" + operation.toString() + "]");
@ -127,22 +127,20 @@ public class OperationManagerImpl implements OperationManager {
public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException { public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException {
try { try {
List<Operation> operations = new ArrayList<Operation>(); OperationManagementDAOFactory.getConnection();
Device device = null;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId);
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId);
} catch (DeviceManagementDAOException e) {
e.printStackTrace();
}
if (device == null) { if (device == null) {
throw new OperationManagementException("Device not found for given device " + throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType());
} }
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList = operationDAO List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList =
.getOperationsForDevice(device.getId()); operationDAO.getOperationsForDevice(device.getId());
Operation operation; Operation operation;
List<Operation> operations = new ArrayList<Operation>();
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) {
operation = OperationDAOUtil.convertOperation(dtoOperation); operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation); operations.add(operation);
@ -152,6 +150,15 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId()
+ "'", e); + "'", e);
} catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving metadata of '" +
deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'");
} finally {
try {
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@ -171,29 +178,22 @@ public class OperationManagerImpl implements OperationManager {
new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>(); new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
try { try {
OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); device = deviceDAO.getDevice(deviceId, tenantId);
if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)){
try {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device,
EnrolmentInfo.Status.ACTIVE);
}catch (DeviceManagementException deviceMgtEx){
String errorMsg = "Error occurred while update enrol status: "+deviceId.toString();
log.error(errorMsg, deviceMgtEx);
throw new OperationManagementException(errorMsg, deviceMgtEx);
}
}
if (device == null) { if (device == null) {
throw new OperationManagementException("Device not found for given device " + throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType());
} }
if (device.getEnrolmentInfo().getStatus() != null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)) {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device, EnrolmentInfo.Status.ACTIVE);
}
dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(),
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
@ -223,12 +223,20 @@ public class OperationManagerImpl implements OperationManager {
+ deviceId.getId(); + deviceId.getId();
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e); throw new OperationManagementException(errorMsg, e);
} catch (DeviceManagementException e) {
throw new OperationManagementException("Error occurred while update enrol status: " +
deviceId.toString(), e);
} finally {
try {
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@Override @Override
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() log.debug("device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType()
+ "]"); + "]");
@ -236,6 +244,8 @@ public class OperationManagerImpl implements OperationManager {
Operation operation = null; Operation operation = null;
Device device; Device device;
try { try {
OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); device = deviceDAO.getDevice(deviceId, tenantId);
@ -246,16 +256,15 @@ public class OperationManagerImpl implements OperationManager {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO
.getNextOperation(device.getId()); .getNextOperation(device.getId());
if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( if (device.getEnrolmentInfo().getStatus() != null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)){ EnrolmentInfo.Status.ACTIVE)) {
try { try {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device, .updateDeviceEnrolmentInfo(device,
EnrolmentInfo.Status.ACTIVE); EnrolmentInfo.Status.ACTIVE);
}catch (DeviceManagementException deviceMgtEx){ } catch (DeviceManagementException e) {
String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); throw new OperationManagementException("Error occurred while update enrol status: '" +
log.error(errorMsg, deviceMgtEx); deviceId.toString() + "'", e);
throw new OperationManagementException(errorMsg, deviceMgtEx);
} }
} }
@ -283,17 +292,19 @@ public class OperationManagerImpl implements OperationManager {
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving next pending operation", e); throw new OperationManagementException("Error occurred while retrieving next pending operation", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e);
+ deviceId.getId(); } finally {
log.error(errorMsg, e); try {
throw new OperationManagementException(errorMsg, e); OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@Override @Override
public void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException { public void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException {
int operationId = operation.getId(); int operationId = operation.getId();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -301,35 +312,44 @@ public class OperationManagerImpl implements OperationManager {
} }
try { try {
OperationManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); Device device = deviceDAO.getDevice(deviceId, tenantId);
if (operation.getStatus() !=null) { if (operation.getStatus() != null) {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
operationDAO.updateOperationStatus(device.getId(), operationId, operationDAO.updateOperationStatus(device.getId(), operationId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status
.valueOf(operation.getStatus().toString())); .valueOf(operation.getStatus().toString()));
OperationManagementDAOFactory.commitTransaction();
} }
if (operation.getOperationResponse() != null){ if (operation.getOperationResponse() != null) {
OperationManagementDAOFactory.beginTransaction();
operationDAO.addOperationResponse(device.getId(), operationId, operation.getOperationResponse()); operationDAO.addOperationResponse(device.getId(), operationId, operation.getOperationResponse());
OperationManagementDAOFactory.commitTransaction();
} }
} catch (OperationManagementDAOException ex) { OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException e) {
try { try {
OperationManagementDAOFactory.rollbackTransaction(); OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) { } catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the update operation transaction", e1); log.warn("Error occurred while roll-backing the update operation transaction", e1);
} }
log.error("Error occurred while updating the operation: " + operationId + " status:" + operation.getStatus(), ex); throw new OperationManagementException("Error occurred while updating the operation: " + operationId +
throw new OperationManagementException("Error occurred while update operation", ex); " status:" + operation.getStatus(), e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
log.error("Error occurred while fetch the device for device identifier: " + deviceId.getId() + " " + try {
"type:" + deviceId.getType(), e); OperationManagementDAOFactory.rollbackTransaction();
throw new OperationManagementException("Error occurred while update operation", e); } catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the update operation transaction", e1);
}
throw new OperationManagementException("Error occurred while fetching the device for device identifier: " +
deviceId.getId() + "type:" + deviceId.getType(), e);
} finally {
try {
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@ -348,14 +368,13 @@ public class OperationManagerImpl implements OperationManager {
lookupOperationDAO(operation).deleteOperation(operationId); lookupOperationDAO(operation).deleteOperation(operationId);
OperationManagementDAOFactory.commitTransaction(); OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException ex) { } catch (OperationManagementDAOException e) {
try { try {
OperationManagementDAOFactory.rollbackTransaction(); OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the delete operation transaction", e); log.warn("Error occurred while roll-backing the delete operation transaction", e1);
} }
log.error("Error occurred while deleting the operation: " + operationId, ex); throw new OperationManagementException("Error occurred while deleting the operation: " + operationId, e);
throw new OperationManagementException("Error occurred while delete operation", ex);
} }
} }
@ -371,6 +390,8 @@ public class OperationManagerImpl implements OperationManager {
} }
try { try {
OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); device = deviceDAO.getDevice(deviceId, tenantId);
if (device == null) { if (device == null) {
@ -383,8 +404,9 @@ public class OperationManagerImpl implements OperationManager {
if (dtoOperation.getType() if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation;
commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO commandOperation =
.getOperation(dtoOperation.getId()); (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO.
getOperation(dtoOperation.getId());
dtoOperation.setEnabled(commandOperation.isEnabled()); dtoOperation.setEnabled(commandOperation.isEnabled());
} else if (dtoOperation.getType() } else if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
@ -413,6 +435,12 @@ public class OperationManagerImpl implements OperationManager {
+ deviceId.getId(); + deviceId.getId();
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e); throw new OperationManagementException(errorMsg, e);
} finally {
try {
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
return operation; return operation;
} }
@ -420,11 +448,11 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus( public List<? extends Operation> getOperationsByDeviceAndStatus(
DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementException { DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementException {
List<Operation> operations = new ArrayList<Operation>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
try { try {
List<Operation> operations = new ArrayList<Operation>(); OperationManagementDAOFactory.getConnection();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
new ArrayList<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation>();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); Device device = deviceDAO.getDevice(deviceId, tenantId);
@ -459,10 +487,14 @@ public class OperationManagerImpl implements OperationManager {
"operations assigned for '" + deviceId.getType() + "' device '" + "operations assigned for '" + deviceId.getType() + "' device '" +
deviceId.getId() + "' and status:" + status.toString(), e); deviceId.getId() + "' and status:" + status.toString(), e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(); "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e);
log.error(errorMsg, e); } finally {
throw new OperationManagementException(errorMsg, e); try {
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
} }
@ -471,6 +503,8 @@ public class OperationManagerImpl implements OperationManager {
Operation operation; Operation operation;
try { try {
OperationManagementDAOFactory.getConnection();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation
(operationId); (operationId);
if (dtoOperation == null) { if (dtoOperation == null) {
@ -494,12 +528,17 @@ public class OperationManagerImpl implements OperationManager {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
} }
operation = OperationDAOUtil.convertOperation(dtoOperation); operation = OperationDAOUtil.convertOperation(dtoOperation);
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the operation with operation Id '" + operationId; String errorMsg = "Error occurred while retrieving the operation with operation Id '" + operationId;
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e); throw new OperationManagementException(errorMsg, e);
} finally {
try {
OperationManagementDAOFactory.closeConnection();
} catch (OperationManagementDAOException e) {
log.warn("Error occurred while closing data source connection", e);
}
} }
return operation; return operation;
} }

Loading…
Cancel
Save