From d0cb80273b474d053f1e8d51742e0c6261ba7c44 Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 18 Aug 2016 11:41:15 +0530 Subject: [PATCH 1/2] Add indexing to mysql --- .../src/main/resources/dbscripts/cdm/mysql.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index 4e9a9ff021..c936fad92d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -96,6 +96,9 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING ( CREATED_TIMESTAMP INTEGER NOT NULL, UPDATED_TIMESTAMP INTEGER NOT NULL, PRIMARY KEY (ID), + KEY `fk_dm_device_operation_mapping_operation` (`OPERATION_ID`), + KEY `IDX_DM_ENROLMENT_OP_MAPPING` (`ENROLMENT_ID`,`OPERATION_ID`), + KEY `ID_DM_ENROLMENT_OP_MAPPING_UPDATED_TIMESTAMP` (`UPDATED_TIMESTAMP`), CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES From 3d19d9ed2e163b1c3b86371e801c41da6f2f2245 Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 18 Aug 2016 14:26:53 +0530 Subject: [PATCH 2/2] Add MySQLOperationDAOImpl for row locking. --- .../dao/OperationManagementDAOFactory.java | 3 +- .../impl/operation/MySQLOperationDAOImpl.java | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java index cdcb005789..e22a83c59e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.*; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.H2OperationDAOImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.MySQLOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.OracleOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.PostgreSQLOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation.SQLServerOperationDAOImpl; @@ -78,7 +79,7 @@ public class OperationManagementDAOFactory { case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: return new H2OperationDAOImpl(); case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: - return new GenericOperationDAOImpl(); + return new MySQLOperationDAOImpl(); default: throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java new file mode 100644 index 0000000000..db625f483c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016a, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; + +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * This class holds the implementation of OperationDAO which can be used to support MySQl db syntax. + */ +public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { + + @Override + public boolean updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) + throws OperationManagementDAOException { + PreparedStatement stmt = null; + boolean isUpdated = false; + try { + long time = System.currentTimeMillis() / 1000; + Connection connection = OperationManagementDAOFactory.getConnection(); + stmt = connection.prepareStatement("SELECT STATUS, UPDATED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING " + + "WHERE ENROLMENT_ID=? and OPERATION_ID=? FOR UPDATE"); + stmt.setString(1, status.toString()); + stmt.setLong(2, time); + if (stmt.execute()) { + OperationManagementDAOUtil.cleanupResources(stmt); + stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS=?, UPDATED_TIMESTAMP=? " + + "WHERE ENROLMENT_ID=? and OPERATION_ID=?"); + stmt.setString(1, status.toString()); + stmt.setLong(2, time); + stmt.setInt(3, enrolmentId); + stmt.setInt(4, operationId); + int numOfRecordsUpdated = stmt.executeUpdate(); + if (numOfRecordsUpdated != 0) { + isUpdated = true; + } + } + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + + "metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + return isUpdated; + } +} \ No newline at end of file