Refractor connection handling in mobile dao factory

revert-dabc3590
manoj 10 years ago
parent 10bf596281
commit 22a951c84d

@ -26,10 +26,10 @@ import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfi
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection; import java.util.HashMap;
import java.sql.SQLException;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Factory class used to create MobileDeviceManagement related DAO objects. * Factory class used to create MobileDeviceManagement related DAO objects.
@ -37,11 +37,26 @@ import java.util.List;
public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface { public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>(); private static Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>();
private static DataSource dataSource; private static boolean isInitialized;
public static void init(MobileDataSourceConfig mobileDataSourceConfig) throws MobileDeviceMgtPluginException { public static void init(Map<String, MobileDataSourceConfig> mobileDataSourceConfigMap)
dataSource = resolveDataSource(mobileDataSourceConfig); throws MobileDeviceMgtPluginException {
DataSource dataSource;
for (String pluginType : mobileDataSourceConfigMap.keySet()) {
if (dataSourceMap.get(pluginType) == null) {
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfigMap.get
(pluginType));
dataSourceMap.put(pluginType, dataSource);
}
}
isInitialized = true;
}
public static void init(String key, MobileDataSourceConfig mobileDataSourceConfig) throws
MobileDeviceMgtPluginException {
DataSource dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfig);
dataSourceMap.put(key, dataSource);
} }
/** /**
@ -80,73 +95,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
return dataSource; return dataSource;
} }
public static void beginTransaction() throws MobileDeviceManagementDAOException { public static Map<String, DataSource> getDataSourceMap() {
try { return dataSourceMap;
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
}
}
public static Connection getConnection() throws MobileDeviceManagementDAOException {
if (currentConnection.get() == null) {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
e);
}
}
return currentConnection.get();
}
public static void commitTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public static void closeConnection() throws MobileDeviceManagementDAOException {
Connection con = currentConnection.get();
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
currentConnection.remove();
} }
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e);
} finally {
closeConnection();
}
}
} }

@ -64,13 +64,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
} }
MobileDeviceManagementDAOFactory.beginTransaction(); AndroidDAOFactory.beginTransaction();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice( status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice(
mobileDevice); mobileDevice);
MobileDeviceManagementDAOFactory.commitTransaction(); AndroidDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { try {
MobileDeviceManagementDAOFactory.rollbackTransaction(); AndroidDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException mobileDAOEx) { } catch (MobileDeviceManagementDAOException mobileDAOEx) {
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString(); String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
log.warn(msg, mobileDAOEx); log.warn(msg, mobileDAOEx);
@ -90,13 +90,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Modifying the Android device enrollment data"); log.debug("Modifying the Android device enrollment data");
} }
MobileDeviceManagementDAOFactory.beginTransaction(); AndroidDAOFactory.beginTransaction();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice); .updateMobileDevice(mobileDevice);
MobileDeviceManagementDAOFactory.commitTransaction(); AndroidDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { try {
MobileDeviceManagementDAOFactory.rollbackTransaction(); AndroidDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException mobileDAOEx) { } catch (MobileDeviceManagementDAOException mobileDAOEx) {
String msg = "Error occurred while roll back the update device transaction :" + device.toString(); String msg = "Error occurred while roll back the update device transaction :" + device.toString();
log.warn(msg, mobileDAOEx); log.warn(msg, mobileDAOEx);
@ -116,13 +116,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Dis-enrolling Android device : " + deviceId); log.debug("Dis-enrolling Android device : " + deviceId);
} }
MobileDeviceManagementDAOFactory.beginTransaction(); AndroidDAOFactory.beginTransaction();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.deleteMobileDevice(deviceId.getId()); .deleteMobileDevice(deviceId.getId());
MobileDeviceManagementDAOFactory.commitTransaction(); AndroidDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { try {
MobileDeviceManagementDAOFactory.rollbackTransaction(); AndroidDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException mobileDAOEx) { } catch (MobileDeviceManagementDAOException mobileDAOEx) {
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
log.warn(msg, mobileDAOEx); log.warn(msg, mobileDAOEx);
@ -200,13 +200,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
log.debug( log.debug(
"updating the details of Android device : " + device.getDeviceIdentifier()); "updating the details of Android device : " + device.getDeviceIdentifier());
} }
MobileDeviceManagementDAOFactory.beginTransaction(); AndroidDAOFactory.beginTransaction();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice); .updateMobileDevice(mobileDevice);
MobileDeviceManagementDAOFactory.commitTransaction(); AndroidDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { try {
MobileDeviceManagementDAOFactory.rollbackTransaction(); AndroidDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException mobileDAOEx) { } catch (MobileDeviceManagementDAOException mobileDAOEx) {
String msg = "Error occurred while roll back the update device info transaction :" + device.toString(); String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
log.warn(msg, mobileDAOEx); log.warn(msg, mobileDAOEx);

@ -48,14 +48,14 @@ public class AndroidFeatureManager implements FeatureManager {
public boolean addFeature(Feature feature) throws DeviceManagementException { public boolean addFeature(Feature feature) throws DeviceManagementException {
try { try {
mobileDeviceManagementDAOFactory.beginTransaction(); AndroidDAOFactory.beginTransaction();
MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature); MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature);
featureDAO.addFeature(mobileFeature); featureDAO.addFeature(mobileFeature);
mobileDeviceManagementDAOFactory.commitTransaction(); AndroidDAOFactory.commitTransaction();
return true; return true;
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { try {
mobileDeviceManagementDAOFactory.rollbackTransaction(); AndroidDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) { } catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e); log.warn("Error occurred while roll-backing the transaction", e);
} }
@ -94,13 +94,13 @@ public class AndroidFeatureManager implements FeatureManager {
public boolean removeFeature(String code) throws DeviceManagementException { public boolean removeFeature(String code) throws DeviceManagementException {
boolean status = false; boolean status = false;
try { try {
mobileDeviceManagementDAOFactory.beginTransaction(); AndroidDAOFactory.beginTransaction();
featureDAO.deleteFeatureByCode(code); featureDAO.deleteFeatureByCode(code);
mobileDeviceManagementDAOFactory.commitTransaction(); AndroidDAOFactory.commitTransaction();
status = true; status = true;
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { try {
mobileDeviceManagementDAOFactory.rollbackTransaction(); AndroidDAOFactory.rollbackTransaction();
} catch (MobileDeviceManagementDAOException e1) { } catch (MobileDeviceManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e); log.warn("Error occurred while roll-backing the transaction", e);
} }

@ -27,11 +27,19 @@ import org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl.AndroidDeviceDAOI
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl.AndroidFeatureDAOImpl; import org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl.AndroidFeatureDAOImpl;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
implements MobileDeviceManagementDAOFactoryInterface { implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(AndroidDAOFactory.class); private static final Log log = LogFactory.getLog(AndroidDAOFactory.class);
protected static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public AndroidDAOFactory() {
this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
}
@Override @Override
public MobileDeviceDAO getMobileDeviceDAO() { public MobileDeviceDAO getMobileDeviceDAO() {
@ -61,4 +69,72 @@ public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
return null; return null;
} }
public static void beginTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
}
}
public static Connection getConnection() throws MobileDeviceManagementDAOException {
if (currentConnection.get() == null) {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
e);
}
}
return currentConnection.get();
}
public static void commitTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public static void closeConnection() throws MobileDeviceManagementDAOException {
Connection con = currentConnection.get();
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
currentConnection.remove();
}
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e);
} finally {
closeConnection();
}
}
} }

@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants; import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidUtils; import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidUtils;
@ -53,7 +54,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
MobileDevice mobileDevice = null; MobileDevice mobileDevice = null;
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " + "SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " +
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION" + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION" +
@ -96,7 +97,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
MobileDeviceManagementDAOFactory.closeConnection(); AndroidDAOFactory.closeConnection();
} }
return mobileDevice; return mobileDevice;
@ -109,7 +110,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String createDBQuery = String createDBQuery =
"INSERT INTO AD_DEVICE(ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, SERIAL, " + "INSERT INTO AD_DEVICE(ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, SERIAL, " +
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, " + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, " +
@ -164,7 +165,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String updateDBQuery = String updateDBQuery =
"UPDATE AD_DEVICE SET GCM_TOKEN = ?, DEVICE_INFO = ?, SERIAL = ?, VENDOR = ?, " + "UPDATE AD_DEVICE SET GCM_TOKEN = ?, DEVICE_INFO = ?, SERIAL = ?, VENDOR = ?, " +
"MAC_ADDRESS = ?, DEVICE_NAME = ?, LATITUDE = ?, LONGITUDE = ?, IMEI = ?, " + "MAC_ADDRESS = ?, DEVICE_NAME = ?, LATITUDE = ?, LONGITUDE = ?, IMEI = ?, " +
@ -219,7 +220,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String deleteDBQuery = String deleteDBQuery =
"DELETE FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?"; "DELETE FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery); stmt = conn.prepareStatement(deleteDBQuery);
@ -253,7 +254,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>(); List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " + "SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " +
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " +
@ -294,7 +295,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
MobileDeviceManagementDAOFactory.closeConnection(); AndroidDAOFactory.closeConnection();
} }
} }

@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO; import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature; import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidFeatureManagementDAOException; import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidFeatureManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants; import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
@ -42,6 +43,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
private static final Log log = LogFactory.getLog(AndroidFeatureDAOImpl.class); private static final Log log = LogFactory.getLog(AndroidFeatureDAOImpl.class);
public AndroidFeatureDAOImpl() { public AndroidFeatureDAOImpl() {
} }
@Override @Override
@ -51,7 +53,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, mobileFeature.getCode()); stmt.setString(1, mobileFeature.getCode());
@ -76,7 +78,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String updateDBQuery = String updateDBQuery =
"UPDATE AD_FEATURE SET NAME = ?, DESCRIPTION = ?" + "UPDATE AD_FEATURE SET NAME = ?, DESCRIPTION = ?" +
"WHERE CODE = ?"; "WHERE CODE = ?";
@ -112,7 +114,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String sql = "DELETE FROM AD_FEATURE WHERE ID = ?"; String sql = "DELETE FROM AD_FEATURE WHERE ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, mblFeatureId); stmt.setInt(1, mblFeatureId);
@ -135,7 +137,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?"; String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, mblFeatureCode); stmt.setString(1, mblFeatureCode);
@ -158,7 +160,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
ResultSet rs = null; ResultSet rs = null;
Connection conn = null; Connection conn = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE ID = ?"; String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, mblFeatureId); stmt.setInt(1, mblFeatureId);
@ -182,7 +184,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
mblFeatureId + "' from the Android database.", e); mblFeatureId + "' from the Android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
MobileDeviceManagementDAOFactory.closeConnection(); AndroidDAOFactory.closeConnection();
} }
} }
@ -194,7 +196,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
Connection conn = null; Connection conn = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?"; String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, mblFeatureCode); stmt.setString(1, mblFeatureCode);
@ -218,7 +220,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
mblFeatureCode + "' from the Android database.", e); mblFeatureCode + "' from the Android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
MobileDeviceManagementDAOFactory.closeConnection(); AndroidDAOFactory.closeConnection();
} }
} }
@ -230,14 +232,14 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
@Override @Override
public List<MobileFeature> getAllFeatures() throws MobileDeviceManagementDAOException { public List<MobileFeature> getAllFeatures() throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
Connection conn = null; Connection conn = null;
List<MobileFeature> features = new ArrayList<MobileFeature>(); List<MobileFeature> features = new ArrayList<MobileFeature>();
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = AndroidDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE"; String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -260,7 +262,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
"android features from the android database.", e); "android features from the android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
MobileDeviceManagementDAOFactory.closeConnection(); AndroidDAOFactory.closeConnection();
} }
} }
} }

@ -18,12 +18,27 @@
*/ */
package org.wso2.carbon.device.mgt.mobile.impl.windows.dao; package org.wso2.carbon.device.mgt.mobile.impl.windows.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
import org.wso2.carbon.device.mgt.mobile.dao.*; import org.wso2.carbon.device.mgt.mobile.dao.*;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
implements MobileDeviceManagementDAOFactoryInterface { implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(WindowsDAOFactory.class);
protected static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public WindowsDAOFactory() {
this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes
.MOBILE_DEVICE_TYPE_WINDOWS);
}
@Override @Override
public MobileDeviceDAO getMobileDeviceDAO() { public MobileDeviceDAO getMobileDeviceDAO() {
@ -54,4 +69,73 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
public MobileFeaturePropertyDAO getFeaturePropertyDAO() { public MobileFeaturePropertyDAO getFeaturePropertyDAO() {
return null; return null;
} }
public static void beginTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
}
}
public static Connection getConnection() throws MobileDeviceManagementDAOException {
if (currentConnection.get() == null) {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
e);
}
}
return currentConnection.get();
}
public static void commitTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
}
}
public static void closeConnection() throws MobileDeviceManagementDAOException {
Connection con = currentConnection.get();
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
}
currentConnection.remove();
}
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e);
} finally {
closeConnection();
}
}
} }

@ -78,9 +78,7 @@ public class MobileDeviceManagementServiceComponent {
Map<String, MobileDataSourceConfig> dsConfigMap = Map<String, MobileDataSourceConfig> dsConfigMap =
config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap(); config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap();
AndroidDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes MobileDeviceManagementDAOFactory.init(dsConfigMap);
.MOBILE_DEVICE_TYPE_ANDROID));
WindowsDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS));
String setupOption = System.getProperty("setup"); String setupOption = System.getProperty("setup");
if (setupOption != null) { if (setupOption != null) {
@ -90,11 +88,10 @@ public class MobileDeviceManagementServiceComponent {
"to begin"); "to begin");
} }
try { try {
DataSource dataSource; for (String pluginType : dsConfigMap.keySet()){
for (MobileDataSourceConfig dataSourceConfig : dsConfigMap.values()) {
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(dataSourceConfig);
MobileDeviceManagementDAOUtil MobileDeviceManagementDAOUtil
.setupMobileDeviceManagementSchema(dataSource); .setupMobileDeviceManagementSchema(MobileDeviceManagementDAOFactory.getDataSourceMap
().get(pluginType));
} }
} catch (MobileDeviceMgtPluginException e) { } catch (MobileDeviceMgtPluginException e) {
log.error("Exception occurred while initializing mobile device management database schema", e); log.error("Exception occurred while initializing mobile device management database schema", e);

Loading…
Cancel
Save