From cf247128b3f8c8aa8ca3151d1662823e0fa15e75 Mon Sep 17 00:00:00 2001 From: manoj Date: Mon, 6 Apr 2015 18:01:57 +0530 Subject: [PATCH] init operation mgt data connection --- .../mgt/dao/OperationManagementDAOFactory.java | 15 ++++++++++++++- .../mgt/dao/impl/CommandOperationDAOImpl.java | 1 + .../operation/mgt/dao/impl/OperationDAOImpl.java | 16 +++++++++------- 3 files changed, 24 insertions(+), 8 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/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 ca0a3b696e0..e7205ca5fe2 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 @@ -36,6 +36,7 @@ public class OperationManagementDAOFactory { private static DataSource dataSource; private static final Log log = LogFactory.getLog(OperationManagementDAOFactory.class); private static ThreadLocal currentConnection = new ThreadLocal(); + private static final Object LOCK = new Object(); public static OperationDAO getCommandOperationDAO() { return new CommandOperationDAOImpl(); @@ -60,6 +61,7 @@ public class OperationManagementDAOFactory { public static void init(DataSource dtSource) { dataSource = dtSource; } + public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); } @@ -72,7 +74,18 @@ public class OperationManagementDAOFactory { } } - public static Connection getConnection() { + public static Connection getConnection() throws OperationManagementDAOException { + if (currentConnection.get() == null) { + synchronized (LOCK) { + + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while retrieving datasource connection", + e); + } + } + } return currentConnection.get(); } 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/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index f36d92a3f58..97462105e22 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -99,6 +99,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = " + "dom.DEVICE_ID) ois ON o.ID = ois.OP_ID ORDER BY " + "o.CREATED_TIMESTAMP ASC) po ON co.OPERATION_ID = po.OPERATION_ID"; + stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); stmt.setString(1, deviceId.getId()); 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/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java index 3870e1c4d9e..5e8527fe9ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; 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 java.sql.*; import java.util.ArrayList; import java.util.Date; @@ -70,7 +71,7 @@ public class OperationDAOImpl implements OperationDAO { stmt.setTimestamp(1, new Timestamp(new Date().getTime())); stmt.setString(2, operation.getStatus().toString()); - stmt.setInt(3,operation.getId()); + stmt.setInt(3, operation.getId()); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); @@ -103,7 +104,7 @@ public class OperationDAOImpl implements OperationDAO { @Override public Operation getOperation(DeviceIdentifier deviceId, - Operation.Status status) throws OperationManagementDAOException { + Operation.Status status) throws OperationManagementDAOException { return null; } @@ -117,11 +118,12 @@ public class OperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS FROM DM_OPERATION o " + - "INNER JOIN (SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " - + - "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND d.DEVICE_IDENTIFICATION = ?) d1 " - + - "INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = dom.DEVICE_ID) ois ON o.ID = ois.OP_ID"; + "INNER JOIN (SELECT dom.OPERATION_ID AS OP_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER " + + "JOIN " + + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND d" + + ".DEVICE_IDENTIFICATION = ?) d1 "+ + "INNER JOIN DM_DEVICE_OPERATION_MAPPING dom ON d1.ID = dom.DEVICE_ID) ois " + + "ON o.ID = ois.OP_ID"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId());