From f0103f27c4cb70447a17e7157eb2836f2c84fd18 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 8 Aug 2019 16:39:18 +0530 Subject: [PATCH] Modify error messages and exception handling --- .../template/dao/DeviceTypeDAOHandler.java | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java index b37eca5f85..6347aa136e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java @@ -70,25 +70,21 @@ public class DeviceTypeDAOHandler { return currentConnection.get(); } - public void commitTransaction() throws DeviceTypeMgtPluginException { + public void commitTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the " + + "transaction via 'beginTransaction'/'openConnection' methods"); + } 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"); - } - } + conn.commit(); } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e); - } finally { - closeConnection(); + log.error("Error occurred while committing the transaction.", e); } } - public void closeConnection() throws DeviceTypeMgtPluginException { + public void closeConnection() { Connection con = currentConnection.get(); if (con != null) { @@ -101,21 +97,17 @@ public class DeviceTypeDAOHandler { currentConnection.remove(); } - public void rollbackTransaction() throws DeviceTypeMgtPluginException { + public void rollbackTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the " + + "transaction via 'beginTransaction'/'openConnection' methods"); + } 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"); - } - } + conn.rollback(); } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e); - } finally { - closeConnection(); + log.error("Error occurred while roll-backing the transaction.", e); } } }