From 124c0b4cf8c51a5430470d8dad47411694173f38 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Mon, 19 Jun 2017 23:05:05 +0530 Subject: [PATCH 1/6] Fixing issues related to oracle database. --- .../mgt/core/dao/impl/GroupDAOImpl.java | 37 ++++++++++++++----- .../mgt/dao/impl/GenericOperationDAOImpl.java | 12 +++--- .../mgt/dao/impl/OperationMappingDAOImpl.java | 7 ++-- .../core/search/mgt/impl/ProcessorImpl.java | 17 +++++---- .../search/mgt/impl/QueryBuilderImpl.java | 31 ++++++++-------- .../impl/feature/GenericFeatureDAOImpl.java | 31 +++++++++------- .../main/resources/dbscripts/cdm/oracle.sql | 2 +- 7 files changed, 81 insertions(+), 56 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java index 7d9947a421b..f57a26886e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GroupDAOImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; @@ -60,7 +61,7 @@ public class GroupDAOImpl implements GroupDAO { return groupId; } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" + - deviceGroup.getName() + "'", e); + deviceGroup.getName() + "'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, null); } @@ -83,7 +84,7 @@ public class GroupDAOImpl implements GroupDAO { stmt.executeUpdate(); } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + - deviceGroup.getName() + "'", e); + deviceGroup.getName() + "'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, null); } @@ -148,7 +149,7 @@ public class GroupDAOImpl implements GroupDAO { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while obtaining information of Device Group '" + - groupId + "'", e); + groupId + "'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } @@ -204,7 +205,12 @@ public class GroupDAOImpl implements GroupDAO { hasOwner = true; } if (hasLimit) { - sql += " LIMIT ?, ?"; + if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE) || + conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL)) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } else { + sql += " LIMIT ?, ?"; + } } int paramIndex = 1; @@ -267,7 +273,12 @@ public class GroupDAOImpl implements GroupDAO { } sql += ")"; if (hasLimit) { - sql += " LIMIT ?, ?"; + if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE) || + conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL)) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } else { + sql += " LIMIT ?, ?"; + } } int paramIndex = 1; @@ -513,7 +524,13 @@ public class GroupDAOImpl implements GroupDAO { " DM_DEVICE d, (" + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + - "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?"; + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE) || + conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL)) { + sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } else { + sql += " LIMIT ?, ?"; + } stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); @@ -531,7 +548,7 @@ public class GroupDAOImpl implements GroupDAO { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while retrieving information of all " + - "registered devices", e); + "registered devices", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } @@ -728,7 +745,7 @@ public class GroupDAOImpl implements GroupDAO { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while getting own groups of user '" - + username + "'", e); + + username + "'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } @@ -753,7 +770,7 @@ public class GroupDAOImpl implements GroupDAO { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while getting own groups of user '" - + username + "'", e); + + username + "'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } @@ -778,7 +795,7 @@ public class GroupDAOImpl implements GroupDAO { } } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while getting own groups count of user '" - + username + "'", e); + + username + "'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } 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/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 9b0c6cb2cd1..71d86d4224d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -320,12 +320,12 @@ public class GenericOperationDAOImpl implements OperationDAO { "de.DEVICE_ID, d.DEVICE_IDENTIFICATION, \n" + "d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, \n" + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, dor.OPERATION_RESPONSE, \n" + - "dor.RECEIVED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING AS eom \n" + - "INNER JOIN DM_OPERATION AS op ON op.ID=eom.OPERATION_ID\n" + - "INNER JOIN DM_ENROLMENT AS de ON de.ID=eom.ENROLMENT_ID\n" + - "INNER JOIN DM_DEVICE AS d ON d.ID=de.DEVICE_ID \n" + - "INNER JOIN DM_DEVICE_TYPE AS dt ON dt.ID=d.DEVICE_TYPE_ID\n" + - "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE AS dor ON dor.ENROLMENT_ID=de.id \n" + + "dor.RECEIVED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING eom \n" + + "INNER JOIN DM_OPERATION op ON op.ID=eom.OPERATION_ID\n" + + "INNER JOIN DM_ENROLMENT de ON de.ID=eom.ENROLMENT_ID\n" + + "INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID \n" + + "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID\n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id \n" + "AND dor.OPERATION_ID = eom.OPERATION_ID\n" + "WHERE eom.OPERATION_ID = ? AND de.TENANT_ID = ?"; 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/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index 82f2cd66e75..04446c882d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -148,11 +148,12 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { //devices to be active at that moment. Hence filtering by 'ACTIVE' & 'UNREACHABLE' device states. String sql = "SELECT ENROLMENT_ID, D.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFIER, MIN(CREATED_TIMESTAMP) " + "AS CREATED_TIMESTAMP, E.STATUS AS ENROLMENT_STATUS, E.TENANT_ID FROM " + - "DM_ENROLMENT_OP_MAPPING AS OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " + + "DM_ENROLMENT_OP_MAPPING OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " + "DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " + "OP.STATUS IN ('"+ Operation.Status.PENDING.name() + "','" + Operation.Status.REPEATED.name() + "') " + "AND OP.CREATED_TIMESTAMP BETWEEN ? AND ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() + - "','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? GROUP BY ENROLMENT_ID"; + "','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? GROUP BY " + + "ENROLMENT_ID, D.DEVICE_IDENTIFICATION,E.STATUS, E.TENANT_ID"; stmt = conn.prepareStatement(sql); stmt.setLong(1, maxDuration); stmt.setLong(2, minDuration); @@ -182,7 +183,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { //We are specifically looking for operation mappings in 'Pending' & 'Repeated' states. Further we want //devices to be active at that moment. Hence filtering by 'ACTIVE' & 'UNREACHABLE' device states. String sql = "SELECT OP.ENROLMENT_ID AS EID, MAX(OP.UPDATED_TIMESTAMP) AS LAST_CONNECTED_TIME FROM " + - "DM_ENROLMENT_OP_MAPPING AS OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " + + "DM_ENROLMENT_OP_MAPPING OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " + "DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " + "OP.STATUS = '" + Operation.Status.COMPLETED.name() + "'" + "AND OP.UPDATED_TIMESTAMP >= ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index b2c779c0379..40f9346338a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -23,6 +23,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; @@ -62,7 +63,7 @@ public class ProcessorImpl implements Processor { @Override public List execute(SearchContext searchContext) throws SearchMgtException { - if(!Utils.validateOperators(searchContext.getConditions())){ + if (!Utils.validateOperators(searchContext.getConditions())) { throw new SearchMgtException("Invalid validator is provided."); } @@ -268,10 +269,10 @@ public class ProcessorImpl implements Processor { } else if (type.getColumnType().equals(ValueType.columnType.INTEGER)) { stmt.setInt(x, type.getIntValue()); x++; - } else if (type.getColumnType().equals(ValueType.columnType.LONG)){ + } else if (type.getColumnType().equals(ValueType.columnType.LONG)) { stmt.setLong(x, type.getLongValue()); x++; - } else if(type.getColumnType().equals(ValueType.columnType.DOUBLE)){ + } else if (type.getColumnType().equals(ValueType.columnType.DOUBLE)) { stmt.setDouble(x, type.getDoubleValue()); x++; } @@ -360,8 +361,9 @@ public class ProcessorImpl implements Processor { try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID IN ("; - if (conn.getMetaData().getDatabaseProductName().contains("H2") || conn.getMetaData() - .getDatabaseProductName().contains("MySQL")) { + if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2) || conn.getMetaData() + .getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL) || + conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE)) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < devices.size(); i++) { builder.append("?,"); @@ -374,8 +376,9 @@ public class ProcessorImpl implements Processor { } else { query += "?) ORDER BY DEVICE_ID"; stmt = conn.prepareStatement(query); - Array array = conn.createArrayOf("INT", Utils.getArrayOfDeviceIds(devices)); - stmt.setArray(1, array); + for (int i = 0; i < devices.size(); i++) { + stmt.setInt(i, devices.get(i).getId()); + } } rs = stmt.executeQuery(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java index 936f8f68f1a..04d52bf5145 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java @@ -113,13 +113,12 @@ public class QueryBuilderImpl implements QueryBuilder { queries.put(Constants.LOCATION, this.processLocation(locCondition)); } - if (log.isDebugEnabled()) { - log.debug("General Query : " + queries.get(Constants.GENERAL)); - log.debug("Property with AND Query : " + queries.get(Constants.PROP_AND)); - log.debug("Property with OR Query : " + queries.get(Constants.PROP_OR)); - log.debug("Location related Query : " + queries.get(Constants.LOCATION)); - } - +// if (log.isDebugEnabled()) { + log.info("General Query : " + queries.get(Constants.GENERAL)); + log.info("Property with AND Query : " + queries.get(Constants.PROP_AND)); + log.info("Property with OR Query : " + queries.get(Constants.PROP_OR)); + log.info("Location related Query : " + queries.get(Constants.LOCATION)); +// } return queries; } @@ -342,10 +341,10 @@ public class QueryBuilderImpl implements QueryBuilder { "DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DE.OWNER, DE.OWNERSHIP, DE.STATUS " + - "AS DE_STATUS FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" + - "LEFT JOIN DM_DEVICE_LOCATION AS DL ON DL.DEVICE_ID=D.ID \n" + - "INNER JOIN DM_DEVICE_TYPE AS DT ON DT.ID=D.DEVICE_TYPE_ID\n" + - "INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + + "AS DE_STATUS FROM DM_DEVICE_DETAIL DD INNER JOIN DM_DEVICE D ON D.ID=DD.DEVICE_ID\n" + + "LEFT JOIN DM_DEVICE_LOCATION DL ON DL.DEVICE_ID=D.ID \n" + + "INNER JOIN DM_DEVICE_TYPE DT ON DT.ID=D.DEVICE_TYPE_ID\n" + + "INNER JOIN DM_ENROLMENT DE ON D.ID=DE.DEVICE_ID\n" + "WHERE D.TENANT_ID = ? "; ValueType type = new ValueType(); @@ -370,11 +369,11 @@ public class QueryBuilderImpl implements QueryBuilder { "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DI.KEY_FIELD, DI.VALUE_FIELD, \n" + "DE.OWNER, DE.OWNERSHIP, DE.STATUS AS DE_STATUS " + - "FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" + - "LEFT JOIN DM_DEVICE_LOCATION AS DL ON DL.DEVICE_ID=D.ID \n" + - "INNER JOIN DM_DEVICE_TYPE AS DT ON DT.ID=D.DEVICE_TYPE_ID\n" + - "INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + - "LEFT JOIN DM_DEVICE_INFO AS DI ON DI.DEVICE_ID=D.ID\n" + + "FROM DM_DEVICE_DETAIL DD INNER JOIN DM_DEVICE D ON D.ID=DD.DEVICE_ID\n" + + "LEFT JOIN DM_DEVICE_LOCATION DL ON DL.DEVICE_ID=D.ID \n" + + "INNER JOIN DM_DEVICE_TYPE DT ON DT.ID=D.DEVICE_TYPE_ID\n" + + "INNER JOIN DM_ENROLMENT DE ON D.ID=DE.DEVICE_ID\n" + + "LEFT JOIN DM_DEVICE_INFO DI ON DI.DEVICE_ID=D.ID\n" + "WHERE D.TENANT_ID = ? "; ValueType type = new ValueType(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java index 371fe39a9d5..aa81dd75f05 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java @@ -38,8 +38,7 @@ import java.util.List; * FeatureDAO implementation for DB engines with ANSI SQL support. */ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { - - private static final Log log = LogFactory.getLog(GenericFeatureDAOImpl.class); + private static final int BATCH_SIZE = 10; @Override public List addProfileFeatures(List features, int profileId) throws @@ -54,31 +53,36 @@ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, new String[] {"id"}); - + stmt = conn.prepareStatement(query, new String[]{"id"}); + int noRecords = 0; for (ProfileFeature feature : features) { stmt.setInt(1, profileId); stmt.setString(2, feature.getFeatureCode()); stmt.setString(3, feature.getDeviceType()); - // if (conn.getMetaData().getDriverName().contains("H2")) { - // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); - // } else { - stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); - //} + stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); stmt.setInt(5, tenantId); stmt.addBatch(); - //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 + noRecords++; + if (noRecords >= BATCH_SIZE && noRecords % BATCH_SIZE == 0) { + stmt.executeBatch(); + generatedKeys = stmt.getGeneratedKeys(); + int i = noRecords - BATCH_SIZE; + while (generatedKeys.next()) { + features.get(i).setId(generatedKeys.getInt(1)); + i++; + } + } } stmt.executeBatch(); - generatedKeys = stmt.getGeneratedKeys(); int i = 0; - + if (noRecords > BATCH_SIZE) { + i = noRecords - BATCH_SIZE; + } while (generatedKeys.next()) { features.get(i).setId(generatedKeys.getInt(1)); i++; } - } catch (SQLException | IOException e) { throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); } finally { @@ -90,4 +94,5 @@ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { private Connection getConnection() throws FeatureManagerDAOException { return PolicyManagementDAOFactory.getConnection(); } + } \ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index 31b7d60fe41..7a29dd47c44 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -49,7 +49,7 @@ CREATE TABLE DM_ROLE_GROUP_MAP ( GROUP_ID NUMBER(10) DEFAULT NULL, ROLE VARCHAR2(45) DEFAULT NULL, TENANT_ID NUMBER(10) DEFAULT 0, - CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID), + CONSTRAINT PK_DM_ROLE_GROUP PRIMARY KEY (ID), CONSTRAINT fk_DM_ROLE_GROUP_MAP_GROUP2 FOREIGN KEY (GROUP_ID) REFERENCES DM_GROUP (ID) ) From ade596d3c46be813ba4c6d3b70823a9a1bb63a3f Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 20 Jun 2017 13:17:03 +0530 Subject: [PATCH 2/6] Merging the AS clause removal queries with generic DAO from oracle. --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 38 ++-- .../operation/OracleOperationDAOImpl.java | 164 ++++-------------- 2 files changed, 51 insertions(+), 151 deletions(-) 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/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 71d86d4224d..f581f76085b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -135,9 +135,9 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + - "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " + - "AND EOM.STATUS = ?;"; + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " + + "AND EOM.STATUS = ?"; stmt = connection.prepareStatement(query); stmt.setInt(1, enrolmentId); stmt.setString(2, operationCode); @@ -149,8 +149,8 @@ public class GenericOperationDAOImpl implements OperationDAO { id = rs.getInt("ID"); } if (id != 0) { - stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + - "UPDATED_TIMESTAMP = ? WHERE ID = ?"); + stmt = connection.prepareStatement( + "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + "UPDATED_TIMESTAMP = ? WHERE ID = ?"); stmt.setString(1, newStatus.toString()); stmt.setLong(2, System.currentTimeMillis() / 1000); stmt.setInt(3, id); @@ -158,8 +158,8 @@ public class GenericOperationDAOImpl implements OperationDAO { } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + - "metadata", e); + throw new OperationManagementDAOException( + "Error occurred while update device mapping operation status " + "metadata", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt); } @@ -173,9 +173,9 @@ public class GenericOperationDAOImpl implements OperationDAO { boolean result = false; try { Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + - "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + - "EOM.STATUS = ?;"; + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + + "EOM.STATUS = ?"; stmt = connection.prepareStatement(query); stmt.setInt(1, enrolmentId); stmt.setString(2, operationCode); @@ -187,16 +187,16 @@ public class GenericOperationDAOImpl implements OperationDAO { id = rs.getInt("ID"); } if (id != 0) { - stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + - "WHERE ID = ?"); + stmt = connection.prepareStatement( + "UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + "WHERE ID = ?"); stmt.setLong(1, System.currentTimeMillis() / 1000); stmt.setInt(2, id); stmt.executeUpdate(); result = true; } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + - "metadata", e); + throw new OperationManagementDAOException( + "Error occurred while update device mapping operation status " + "metadata", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt); } @@ -548,9 +548,9 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING AS m \n" + - "INNER JOIN DM_ENROLMENT AS d ON m.ENROLMENT_ID = d.ID \n" + - "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?;"; + String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING m \n" + + "INNER JOIN DM_ENROLMENT d ON m.ENROLMENT_ID = d.ID \n" + + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setLong(1, timestamp); stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); @@ -559,8 +559,8 @@ public class GenericOperationDAOImpl implements OperationDAO { return rs.getInt("COUNT"); } } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while getting the activity count from " + - "the database.", e); + throw new OperationManagementDAOException( + "Error occurred while getting the activity count from " + "the database.", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); } 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java index dd810cab2d5..88864447d3e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java @@ -138,77 +138,47 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { } @Override - public void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, - Operation.Status existingStatus, Operation.Status newStatus) throws OperationManagementDAOException { + public Map> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, + int limit) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; + OperationMapping operationMapping; + Map> operationMappingsTenantMap = new HashMap<>(); try { - Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " - + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " - + "AND EOM.STATUS = ?"; - stmt = connection.prepareStatement(query); - stmt.setInt(1, enrolmentId); - stmt.setString(2, operationCode); - stmt.setString(3, existingStatus.toString()); - // This will return only one result always. - rs = stmt.executeQuery(); - int id = 0; - while (rs.next()) { - id = rs.getInt("ID"); - } - if (id != 0) { - stmt = connection.prepareStatement( - "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + "UPDATED_TIMESTAMP = ? WHERE ID = ?"); - stmt.setString(1, newStatus.toString()); - stmt.setLong(2, System.currentTimeMillis() / 1000); - stmt.setInt(3, id); - stmt.executeUpdate(); - } - - } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while update device mapping operation status " + "metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - } + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT op.ENROLMENT_ID, op.OPERATION_ID, d.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE, d" + + ".TENANT_ID FROM DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op, DM_DEVICE_TYPE dt WHERE op.STATUS = ? " + + "AND op.PUSH_NOTIFICATION_STATUS = ? AND d.DEVICE_TYPE_ID = dt.ID AND d.ID=op.ENROLMENT_ID AND " + + "ROWNUM <= ? ORDER BY op.OPERATION_ID"; - @Override - public boolean updateTaskOperation(int enrolmentId, String operationCode) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - boolean result = false; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " - + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " - + "EOM.STATUS = ?"; - stmt = connection.prepareStatement(query); - stmt.setInt(1, enrolmentId); - stmt.setString(2, operationCode); - stmt.setString(3, Operation.Status.PENDING.toString()); - // This will return only one result always. + stmt = conn.prepareStatement(sql); + stmt.setString(1, opStatus.toString()); + stmt.setString(2, pushNotificationStatus.toString()); + stmt.setInt(3, limit); rs = stmt.executeQuery(); - int id = 0; - if (rs.next()) { - id = rs.getInt("ID"); - } - if (id != 0) { - stmt = connection.prepareStatement( - "UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + "WHERE ID = ?"); - stmt.setLong(1, System.currentTimeMillis() / 1000); - stmt.setInt(2, id); - stmt.executeUpdate(); - result = true; + while (rs.next()) { + int tenantID = rs.getInt("TENANT_ID"); + List operationMappings = operationMappingsTenantMap.get(tenantID); + if (operationMappings == null) { + operationMappings = new LinkedList<>(); + operationMappingsTenantMap.put(tenantID, operationMappings); + } + operationMapping = new OperationMapping(); + operationMapping.setOperationId(rs.getInt("OPERATION_ID")); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); + operationMapping.setDeviceIdentifier(deviceIdentifier); + operationMapping.setEnrollmentId(rs.getInt("ENROLMENT_ID")); + operationMapping.setTenantId(tenantID); + operationMappings.add(operationMapping); } } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while update device mapping operation status " + "metadata", e); + throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e); } finally { - OperationManagementDAOUtil.cleanupResources(stmt); + OperationManagementDAOUtil.cleanupResources(stmt, rs); } - return result; + return operationMappingsTenantMap; } @Override @@ -340,74 +310,4 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { } return activities; } - - @Override - public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(*) COUNT FROM DM_ENROLMENT_OP_MAPPING m \n" - + "INNER JOIN DM_ENROLMENT d ON m.ENROLMENT_ID = d.ID \n" - + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); - stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); - rs = stmt.executeQuery(); - if (rs.next()) { - return rs.getInt("COUNT"); - } - } catch (SQLException e) { - throw new OperationManagementDAOException( - "Error occurred while getting the activity count from " + "the database.", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - return 0; - } - - - @Override - public Map> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, - int limit) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; - OperationMapping operationMapping; - Map> operationMappingsTenantMap = new HashMap<>(); - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT op.ENROLMENT_ID, op.OPERATION_ID, d.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE, d" + - ".TENANT_ID FROM DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op, DM_DEVICE_TYPE dt WHERE op.STATUS = ? " + - "AND op.PUSH_NOTIFICATION_STATUS = ? AND d.DEVICE_TYPE_ID = dt.ID AND d.ID=op.ENROLMENT_ID AND " + - "ROWNUM <= ? ORDER BY op.OPERATION_ID"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, opStatus.toString()); - stmt.setString(2, pushNotificationStatus.toString()); - stmt.setInt(3, limit); - rs = stmt.executeQuery(); - while (rs.next()) { - int tenantID = rs.getInt("TENANT_ID"); - List operationMappings = operationMappingsTenantMap.get(tenantID); - if (operationMappings == null) { - operationMappings = new LinkedList<>(); - operationMappingsTenantMap.put(tenantID, operationMappings); - } - operationMapping = new OperationMapping(); - operationMapping.setOperationId(rs.getInt("OPERATION_ID")); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); - deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); - operationMapping.setDeviceIdentifier(deviceIdentifier); - operationMapping.setEnrollmentId(rs.getInt("ENROLMENT_ID")); - operationMapping.setTenantId(tenantID); - operationMappings.add(operationMapping); - } - } catch (SQLException e) { - throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - return operationMappingsTenantMap; - } } \ No newline at end of file From f59202c0c32a0b7ec39efd4b6aed5dff3a6a6a7b Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 20 Jun 2017 13:46:39 +0530 Subject: [PATCH 3/6] Reverting back to the debug logs. --- .../mgt/core/search/mgt/impl/QueryBuilderImpl.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java index 04d52bf5145..d5d0b1700a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java @@ -113,12 +113,12 @@ public class QueryBuilderImpl implements QueryBuilder { queries.put(Constants.LOCATION, this.processLocation(locCondition)); } -// if (log.isDebugEnabled()) { - log.info("General Query : " + queries.get(Constants.GENERAL)); - log.info("Property with AND Query : " + queries.get(Constants.PROP_AND)); - log.info("Property with OR Query : " + queries.get(Constants.PROP_OR)); - log.info("Location related Query : " + queries.get(Constants.LOCATION)); -// } + if (log.isDebugEnabled()) { + log.debug("General Query : " + queries.get(Constants.GENERAL)); + log.debug("Property with AND Query : " + queries.get(Constants.PROP_AND)); + log.debug("Property with OR Query : " + queries.get(Constants.PROP_OR)); + log.debug("Location related Query : " + queries.get(Constants.LOCATION)); + } return queries; } From ce5ca09e51a891013bf2cf1016c22dbfe39bd710 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 20 Jun 2017 16:15:12 +0530 Subject: [PATCH 4/6] Adding oracle server specific impl. --- .../core/dao/PolicyManagementDAOFactory.java | 2 + .../feature/OracleServerFeatureDAOImpl.java | 91 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index 026335ccf73..f17ec30d059 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -29,6 +29,7 @@ import org.wso2.carbon.policy.mgt.core.dao.impl.MonitoringDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.feature.GenericFeatureDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.feature.OracleServerFeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.feature.SQLServerFeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; @@ -77,6 +78,7 @@ public class PolicyManagementDAOFactory { case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: return new SQLServerFeatureDAOImpl(); case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + return new OracleServerFeatureDAOImpl(); case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java new file mode 100644 index 00000000000..91add5615cd --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java @@ -0,0 +1,91 @@ +/* +* Copyright (c) 2017, 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.policy.mgt.core.dao.impl.feature; + +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class OracleServerFeatureDAOImpl extends AbstractFeatureDAO { + private static int BATCH_SIZE = 10; + + @Override + public List addProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(query, new String[]{"id"}); + int noRecords = 0; + for (ProfileFeature feature : features) { + stmt.setInt(1, profileId); + stmt.setString(2, feature.getFeatureCode()); + stmt.setString(3, feature.getDeviceType()); + stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + stmt.setInt(5, tenantId); + stmt.addBatch(); + noRecords++; + if (noRecords >= BATCH_SIZE && noRecords % BATCH_SIZE == 0) { + stmt.executeBatch(); + generatedKeys = stmt.getGeneratedKeys(); + int i = noRecords - this.BATCH_SIZE; + while (generatedKeys.next()) { + features.get(i).setId(generatedKeys.getInt(1)); + i++; + } + } + } + stmt.executeBatch(); + generatedKeys = stmt.getGeneratedKeys(); + int i = 0; + if (noRecords > BATCH_SIZE) { + i = noRecords - BATCH_SIZE; + } + while (generatedKeys.next()) { + features.get(i).setId(generatedKeys.getInt(1)); + i++; + } + } catch (SQLException | IOException e) { + throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); + } + return features; + } + + private Connection getConnection() throws FeatureManagerDAOException { + return PolicyManagementDAOFactory.getConnection(); + } +} From f2ea96efde38be3f727aeb8221565605fbe58f99 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 20 Jun 2017 16:16:10 +0530 Subject: [PATCH 5/6] Reverting the changes done to GenericFeatureDAOImpl. --- .../impl/feature/GenericFeatureDAOImpl.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java index aa81dd75f05..bee766f16d4 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java @@ -38,7 +38,8 @@ import java.util.List; * FeatureDAO implementation for DB engines with ANSI SQL support. */ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { - private static final int BATCH_SIZE = 10; + + private static final Log log = LogFactory.getLog(GenericFeatureDAOImpl.class); @Override public List addProfileFeatures(List features, int profileId) throws @@ -53,36 +54,31 @@ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, new String[]{"id"}); - int noRecords = 0; + stmt = conn.prepareStatement(query, new String[] {"id"}); + for (ProfileFeature feature : features) { stmt.setInt(1, profileId); stmt.setString(2, feature.getFeatureCode()); stmt.setString(3, feature.getDeviceType()); + // if (conn.getMetaData().getDriverName().contains("H2")) { + // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + // } else { stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + //} stmt.setInt(5, tenantId); stmt.addBatch(); - noRecords++; - if (noRecords >= BATCH_SIZE && noRecords % BATCH_SIZE == 0) { - stmt.executeBatch(); - generatedKeys = stmt.getGeneratedKeys(); - int i = noRecords - BATCH_SIZE; - while (generatedKeys.next()) { - features.get(i).setId(generatedKeys.getInt(1)); - i++; - } - } + //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 } stmt.executeBatch(); + generatedKeys = stmt.getGeneratedKeys(); int i = 0; - if (noRecords > BATCH_SIZE) { - i = noRecords - BATCH_SIZE; - } + while (generatedKeys.next()) { features.get(i).setId(generatedKeys.getInt(1)); i++; } + } catch (SQLException | IOException e) { throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); } finally { @@ -94,5 +90,4 @@ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { private Connection getConnection() throws FeatureManagerDAOException { return PolicyManagementDAOFactory.getConnection(); } - } \ No newline at end of file From 4018d582ed9ed4a7a39c7855615526073166eb91 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 20 Jun 2017 16:17:07 +0530 Subject: [PATCH 6/6] Adding comments to the batch size config. --- .../mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java index 91add5615cd..124e5a7ee44 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/OracleServerFeatureDAOImpl.java @@ -32,6 +32,9 @@ import java.sql.SQLException; import java.util.List; public class OracleServerFeatureDAOImpl extends AbstractFeatureDAO { + /** + * Batch sizes greater than 10 throws array out of bound exception. + */ private static int BATCH_SIZE = 10; @Override