Refractor DB connections and resultsets

revert-dabc3590
manoj 10 years ago
parent d416a38eaf
commit 10bf596281

@ -20,19 +20,16 @@ package org.wso2.carbon.device.mgt.mobile.dao;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.common.MobileDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition; 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.config.datasource.MobileDataSourceConfig;
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.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
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.
@ -83,7 +80,6 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
return dataSource; return dataSource;
} }
public static void beginTransaction() throws MobileDeviceManagementDAOException { public static void beginTransaction() throws MobileDeviceManagementDAOException {
try { try {
Connection conn = dataSource.getConnection(); Connection conn = dataSource.getConnection();
@ -105,6 +101,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
} }
return currentConnection.get(); return currentConnection.get();
} }
public static void commitTransaction() throws MobileDeviceManagementDAOException { public static void commitTransaction() throws MobileDeviceManagementDAOException {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();

@ -64,11 +64,18 @@ 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();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice( status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice(
mobileDevice); mobileDevice);
MobileDeviceManagementDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
String msg = "Error while enrolling the Android device : " + try {
device.getDeviceIdentifier(); 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); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
} }
@ -83,9 +90,17 @@ 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();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice); .updateMobileDevice(mobileDevice);
MobileDeviceManagementDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } 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 : " + String msg = "Error while updating the enrollment of the Android device : " +
device.getDeviceIdentifier(); device.getDeviceIdentifier();
log.error(msg, e); log.error(msg, e);
@ -101,9 +116,17 @@ 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();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.deleteMobileDevice(deviceId.getId()); .deleteMobileDevice(deviceId.getId());
MobileDeviceManagementDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } 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(); String msg = "Error while removing the Android device : " + deviceId.getId();
log.error(msg, e); log.error(msg, e);
throw new DeviceManagementException(msg, e); throw new DeviceManagementException(msg, e);
@ -177,9 +200,17 @@ 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();
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
.updateMobileDevice(mobileDevice); .updateMobileDevice(mobileDevice);
MobileDeviceManagementDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } 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 = String msg =
"Error while updating the Android device : " + device.getDeviceIdentifier(); "Error while updating the Android device : " + device.getDeviceIdentifier();
log.error(msg, e); log.error(msg, e);

@ -70,11 +70,6 @@ public class AndroidFeatureManager implements FeatureManager {
Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature); Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature);
return feature; return feature;
} catch (MobileDeviceManagementDAOException e) { } 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); throw new DeviceManagementException("Error occurred while retrieving the feature", e);
} }
} }
@ -90,13 +85,8 @@ public class AndroidFeatureManager implements FeatureManager {
} }
return featureList; return featureList;
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { throw new DeviceManagementException("Error occurred while retrieving the list of features registered for " +
mobileDeviceManagementDAOFactory.rollbackTransaction(); "Android platform", e);
} 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);
} }
} }

@ -51,6 +51,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
MobileDevice mobileDevice = null; MobileDevice mobileDevice = null;
ResultSet resultSet = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String selectDBQuery = String selectDBQuery =
@ -59,7 +60,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
" FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?"; " FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, mblDeviceId); stmt.setString(1, mblDeviceId);
ResultSet resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
mobileDevice = new MobileDevice(); mobileDevice = new MobileDevice();
@ -94,7 +95,8 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
log.error(msg, e); log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
MobileDeviceManagementDAOFactory.closeConnection();
} }
return mobileDevice; return mobileDevice;
@ -150,7 +152,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
log.error(msg, e); log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@ -205,7 +207,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
log.error(msg, e); log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@ -235,7 +237,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
log.error(msg, e); log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@ -243,10 +245,13 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
@Override @Override
public List<MobileDevice> getAllMobileDevices() public List<MobileDevice> getAllMobileDevices()
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null;
MobileDevice mobileDevice; MobileDevice mobileDevice;
List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>(); List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String selectDBQuery = String selectDBQuery =
@ -254,7 +259,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " +
"FROM AD_DEVICE"; "FROM AD_DEVICE";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
ResultSet resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
mobileDevice = new MobileDevice(); mobileDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(resultSet.getString(AndroidPluginConstants. mobileDevice.setMobileDeviceId(resultSet.getString(AndroidPluginConstants.
@ -288,7 +293,8 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
log.error(msg, e); log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
MobileDeviceManagementDAOFactory.closeConnection();
} }
} }

@ -46,10 +46,10 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
@Override @Override
public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
try { try {
conn = MobileDeviceManagementDAOFactory.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; 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 '" + "Error occurred while adding android feature '" +
mobileFeature.getName() + "' into the metadata repository", e); mobileFeature.getName() + "' into the metadata repository", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@ -100,13 +100,14 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
log.error(msg, e); log.error(msg, e);
throw new AndroidFeatureManagementDAOException(msg, e); throw new AndroidFeatureManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@Override @Override
public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
@ -122,7 +123,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
"Error occurred while deleting android feature '" + "Error occurred while deleting android feature '" +
mblFeatureId + "' from Android database.", e); mblFeatureId + "' from Android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@ -145,7 +146,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
"Error occurred while deleting android feature '" + "Error occurred while deleting android feature '" +
mblFeatureCode + "' from Android database.", e); mblFeatureCode + "' from Android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return status; return status;
} }
@ -180,7 +181,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
"Error occurred while retrieving android feature '" + "Error occurred while retrieving android feature '" +
mblFeatureId + "' from the Android database.", e); mblFeatureId + "' from the Android database.", e);
} finally { } 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 '" + "Error occurred while retrieving android feature '" +
mblFeatureCode + "' from the Android database.", e); mblFeatureCode + "' from the Android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
MobileDeviceManagementDAOFactory.closeConnection();
} }
} }
@ -227,6 +230,7 @@ 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;
@ -255,7 +259,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
throw new AndroidFeatureManagementDAOException("Error occurred while retrieving all " + throw new AndroidFeatureManagementDAOException("Error occurred while retrieving all " +
"android features from the android database.", e); "android features from the android database.", e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
MobileDeviceManagementDAOFactory.closeConnection();
} }
} }
} }

Loading…
Cancel
Save