Fix deadlock in device application list update

4.x.x
Charitha Goonetilleke 5 years ago
parent 311ddc307e
commit dac5c2b511

@ -2650,7 +2650,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
int deviceCount = 0; int deviceCount = 0;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS != 'REMOVED'"; String sql = "SELECT COUNT(e.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e WHERE STATUS = 'ACTIVE' " +
"OR STATUS = 'UNREACHABLE'";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();

@ -88,7 +88,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " + stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " +
"VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ? " + "VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ? " +
"WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?"); "WHERE ID = ?");
for (Application application : applications) { for (Application application : applications) {
stmt.setString(1, application.getName()); stmt.setString(1, application.getName());
@ -100,10 +100,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(7, application.getImageUrl()); stmt.setString(7, application.getImageUrl());
stmt.setInt(8, application.getMemoryUsage()); stmt.setInt(8, application.getMemoryUsage());
stmt.setBoolean(9, application.isActive()); stmt.setBoolean(9, application.isActive());
stmt.setString(10, application.getApplicationIdentifier()); stmt.setInt(10, application.getId());
stmt.setInt(11, deviceId);
stmt.setInt(12, enrolmentId);
stmt.setInt(13, tenantId);
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
@ -123,14 +120,10 @@ public class ApplicationDAOImpl implements ApplicationDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? " + stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE ID = ?");
"AND ENROLMENT_ID = ? AND TENANT_ID = ?");
for (Application app : apps) { for (Application app : apps) {
stmt.setString(1, app.getApplicationIdentifier()); stmt.setInt(1, app.getId());
stmt.setInt(2, deviceId);
stmt.setInt(3, enrolmentId);
stmt.setInt(4, tenantId);
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();

@ -834,13 +834,14 @@ public class OperationManagerImpl implements OperationManager {
public void updateOperation(int enrolmentId, Operation operation, DeviceIdentifier deviceId) public void updateOperation(int enrolmentId, Operation operation, DeviceIdentifier deviceId)
throws OperationManagementException { throws OperationManagementException {
int operationId = operation.getId(); int operationId = operation.getId();
boolean isOperationUpdated = false;
try { try {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
if (operation.getStatus() != null) { if (operation.getStatus() != null) {
int failAttempts = 0; int failAttempts = 0;
while (true) { while (true) {
try { try {
operationDAO.updateOperationStatus(enrolmentId, operationId, isOperationUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt. org.wso2.carbon.device.mgt.core.dto.operation.mgt.
Operation.Status.valueOf(operation.getStatus(). Operation.Status.valueOf(operation.getStatus().
toString())); toString()));
@ -865,7 +866,10 @@ public class OperationManagerImpl implements OperationManager {
} }
} }
} }
if (operation.getOperationResponse() != null) { if (!isOperationUpdated) {
log.warn("Operation " + operationId + "'s status is not updated");
}
if (isOperationUpdated && operation.getOperationResponse() != null) {
OperationMonitoringTaskConfig operationMonitoringTaskConfig = DeviceManagementDataHolder OperationMonitoringTaskConfig operationMonitoringTaskConfig = DeviceManagementDataHolder
.getInstance().getDeviceManagementProvider().getDeviceMonitoringConfig(deviceId.getType()); .getInstance().getDeviceManagementProvider().getDeviceMonitoringConfig(deviceId.getType());
List<MonitoringOperation> monitoringOperations = operationMonitoringTaskConfig.getMonitoringOperation(); List<MonitoringOperation> monitoringOperations = operationMonitoringTaskConfig.getMonitoringOperation();
@ -901,7 +905,7 @@ public class OperationManagerImpl implements OperationManager {
} }
} }
OperationResponseMeta responseMeta = null; OperationResponseMeta responseMeta = null;
if (operation.getOperationResponse() != null) { if (isOperationUpdated && operation.getOperationResponse() != null) {
int failAttempts = 0; int failAttempts = 0;
while (true) { while (true) {
try { try {

@ -182,6 +182,9 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE (
DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_RES_RT' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE'))
CREATE INDEX IDX_DM_RES_RT ON DM_DEVICE_OPERATION_RESPONSE(RECEIVED_TIMESTAMP);
IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENID_OPID' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE')) IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENID_OPID' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE'))
CREATE INDEX IDX_ENID_OPID ON DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, ENROLMENT_ID); CREATE INDEX IDX_ENID_OPID ON DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, ENROLMENT_ID);
@ -190,11 +193,12 @@ CREATE INDEX IDX_DM_EN_OP_MAP_RES ON DM_DEVICE_OPERATION_RESPONSE(EN_OP_MAP_ID);
IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_OPERATION_RESPONSE_LARGE]') AND TYPE IN (N'U')) IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_OPERATION_RESPONSE_LARGE]') AND TYPE IN (N'U'))
CREATE TABLE DM_DEVICE_OPERATION_RESPONSE_LARGE ( CREATE TABLE DM_DEVICE_OPERATION_RESPONSE_LARGE (
ID INTEGER IDENTITY(1,1) NOT NULL, ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL,
EN_OP_MAP_ID INTEGER NOT NULL, EN_OP_MAP_ID INTEGER NOT NULL,
OPERATION_RESPONSE VARBINARY(MAX) DEFAULT NULL, OPERATION_RESPONSE VARBINARY(MAX) DEFAULT NULL,
RECEIVED_TIMESTAMP DATETIME2 DEFAULT NULL RECEIVED_TIMESTAMP DATETIME2 DEFAULT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_DEVICE_OPERATION_RESP_LARGE_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES CONSTRAINT FK_DM_DEVICE_OPERATION_RESP_LARGE_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
@ -419,6 +423,9 @@ CREATE TABLE DM_APPLICATION (
DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_APPLICATION' AND OBJECT_ID = OBJECT_ID('DM_APPLICATION'))
CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID);
-- POLICY RELATED TABLES FINISHED -- -- POLICY RELATED TABLES FINISHED --
-- POLICY AND DEVICE GROUP MAPPING -- -- POLICY AND DEVICE GROUP MAPPING --

@ -439,6 +439,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION (
DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB; )ENGINE = InnoDB;
CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID);
-- END OF POLICY RELATED TABLES -- -- END OF POLICY RELATED TABLES --
-- POLICY AND DEVICE GROUP MAPPING -- -- POLICY AND DEVICE GROUP MAPPING --

Loading…
Cancel
Save