From 10bf596281d4c4b73bb5acf2cfc2f26d957ebede Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 14 May 2015 20:44:40 +0530 Subject: [PATCH] Refractor DB connections and resultsets --- .../dao/MobileDeviceManagementDAOFactory.java | 11 +++--- .../impl/android/AndroidDeviceManager.java | 35 +++++++++++++++++-- .../impl/android/AndroidFeatureManager.java | 14 ++------ .../dao/impl/AndroidDeviceDAOImpl.java | 20 +++++++---- .../dao/impl/AndroidFeatureDAOImpl.java | 21 ++++++----- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java index 3ad5cbfcc..ddee078a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java @@ -20,19 +20,16 @@ package org.wso2.carbon.device.mgt.mobile.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.DataSourceNotAvailableException; import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException; import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; + import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -import java.util.HashMap; import java.util.Hashtable; import java.util.List; -import java.util.Map; /** * Factory class used to create MobileDeviceManagement related DAO objects. @@ -43,7 +40,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa private static ThreadLocal currentConnection = new ThreadLocal(); private static DataSource dataSource; - public static void init(MobileDataSourceConfig mobileDataSourceConfig ) throws MobileDeviceMgtPluginException { + public static void init(MobileDataSourceConfig mobileDataSourceConfig) throws MobileDeviceMgtPluginException { dataSource = resolveDataSource(mobileDataSourceConfig); } @@ -83,7 +80,6 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa return dataSource; } - public static void beginTransaction() throws MobileDeviceManagementDAOException { try { Connection conn = dataSource.getConnection(); @@ -105,6 +101,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa } return currentConnection.get(); } + public static void commitTransaction() throws MobileDeviceManagementDAOException { try { Connection conn = currentConnection.get(); @@ -119,7 +116,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa } catch (SQLException e) { throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e); } finally { - closeConnection(); + closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java index 925d6b1e1..02039191e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java @@ -64,11 +64,18 @@ public class AndroidDeviceManager implements DeviceMgtService { if (log.isDebugEnabled()) { log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice( mobileDevice); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { - String msg = "Error while enrolling the Android device : " + - device.getDeviceIdentifier(); + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the device enrol transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while enrolling the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -83,9 +90,17 @@ public class AndroidDeviceManager implements DeviceMgtService { if (log.isDebugEnabled()) { log.debug("Modifying the Android device enrollment data"); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() .updateMobileDevice(mobileDevice); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } String msg = "Error while updating the enrollment of the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); @@ -101,9 +116,17 @@ public class AndroidDeviceManager implements DeviceMgtService { if (log.isDebugEnabled()) { log.debug("Dis-enrolling Android device : " + deviceId); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() .deleteMobileDevice(deviceId.getId()); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); + log.warn(msg, mobileDAOEx); + } String msg = "Error while removing the Android device : " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); @@ -177,9 +200,17 @@ public class AndroidDeviceManager implements DeviceMgtService { log.debug( "updating the details of Android device : " + device.getDeviceIdentifier()); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() .updateMobileDevice(mobileDevice); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device info transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } String msg = "Error while updating the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java index cb7373279..ed6961864 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java @@ -70,11 +70,6 @@ public class AndroidFeatureManager implements FeatureManager { Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature); return feature; } catch (MobileDeviceManagementDAOException e) { - try { - mobileDeviceManagementDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException e1) { - log.warn("Error occurred while roll-backing the transaction", e); - } throw new DeviceManagementException("Error occurred while retrieving the feature", e); } } @@ -90,13 +85,8 @@ public class AndroidFeatureManager implements FeatureManager { } return featureList; } catch (MobileDeviceManagementDAOException e) { - try { - mobileDeviceManagementDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException e1) { - log.warn("Error occurred while roll-backing the transaction", e); - } - throw new DeviceManagementException("Error occurred while retrieving the list of " + - "features registered for Android platform", e); + throw new DeviceManagementException("Error occurred while retrieving the list of features registered for " + + "Android platform", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java index 9b3c6a3ff..1c5b9133b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java @@ -51,6 +51,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ Connection conn = null; PreparedStatement stmt = null; MobileDevice mobileDevice = null; + ResultSet resultSet = null; try { conn = MobileDeviceManagementDAOFactory.getConnection(); String selectDBQuery = @@ -59,7 +60,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ " FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?"; stmt = conn.prepareStatement(selectDBQuery); stmt.setString(1, mblDeviceId); - ResultSet resultSet = stmt.executeQuery(); + resultSet = stmt.executeQuery(); if (resultSet.next()) { mobileDevice = new MobileDevice(); @@ -94,7 +95,8 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); + MobileDeviceManagementDAOFactory.closeConnection(); } return mobileDevice; @@ -150,7 +152,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -205,7 +207,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -235,7 +237,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -243,10 +245,13 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ @Override public List getAllMobileDevices() throws MobileDeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; + ResultSet resultSet = null; MobileDevice mobileDevice; List mobileDevices = new ArrayList(); + try { conn = MobileDeviceManagementDAOFactory.getConnection(); String selectDBQuery = @@ -254,7 +259,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " + "FROM AD_DEVICE"; stmt = conn.prepareStatement(selectDBQuery); - ResultSet resultSet = stmt.executeQuery(); + resultSet = stmt.executeQuery(); while (resultSet.next()) { mobileDevice = new MobileDevice(); mobileDevice.setMobileDeviceId(resultSet.getString(AndroidPluginConstants. @@ -288,7 +293,8 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); + MobileDeviceManagementDAOFactory.closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java index a223b6f00..fda907230 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java @@ -46,10 +46,10 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { @Override public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; boolean status = false; Connection conn = null; - try { conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; @@ -65,7 +65,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while adding android feature '" + mobileFeature.getName() + "' into the metadata repository", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -100,13 +100,14 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { log.error(msg, e); throw new AndroidFeatureManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @Override public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; boolean status = false; Connection conn = null; @@ -122,7 +123,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while deleting android feature '" + mblFeatureId + "' from Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -145,7 +146,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while deleting android feature '" + mblFeatureCode + "' from Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -180,7 +181,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while retrieving android feature '" + mblFeatureId + "' from the Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.closeConnection(); } } @@ -215,7 +217,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while retrieving android feature '" + mblFeatureCode + "' from the Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.closeConnection(); } } @@ -227,6 +230,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { @Override public List getAllFeatures() throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; ResultSet rs = null; Connection conn = null; @@ -255,7 +259,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { throw new AndroidFeatureManagementDAOException("Error occurred while retrieving all " + "android features from the android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.closeConnection(); } } }