From 6605c1372be3dfdd57aa5739a1a741b447e37e3a Mon Sep 17 00:00:00 2001 From: Chathura Ekanayake Date: Tue, 1 Aug 2017 13:34:33 +0530 Subject: [PATCH] improvements to database transaction handling and implementing application delete. --- .../mgt/core/dao/impl/AbstractDAOImpl.java | 5 + .../application/MySQLApplicationDAOImpl.java | 8 +- .../mgt/core/impl/ApplicationManagerImpl.java | 10 +- .../mgt/core/util/ConnectionManagerUtil.java | 113 ++++++++++++++++++ 4 files changed, 126 insertions(+), 10 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java index b302abb70c4..a85ce946a59 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java @@ -25,6 +25,11 @@ import java.sql.Connection; public abstract class AbstractDAOImpl { + protected Connection getDBConnection() throws DBConnectionException { + return ConnectionManagerUtil.getDBConnection(); + } + + @Deprecated protected Connection getConnection() throws DBConnectionException { return ConnectionManagerUtil.getConnection(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java index dd8ee04074d..5e9c74370b4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/MySQLApplicationDAOImpl.java @@ -195,7 +195,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { PreparedStatement stmt = null; ResultSet rs = null; try { - conn = this.getConnection(); + conn = this.getDBConnection(); String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, uuid); @@ -220,7 +220,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { int id = 0; try { - conn = this.getConnection(); + conn = this.getDBConnection(); sql += "SELECT ID FROM APPM_APPLICATION WHERE UUID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, uuid); @@ -360,7 +360,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { PreparedStatement stmt = null; ResultSet rs = null; try { - conn = this.getConnection(); + conn = this.getDBConnection(); String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, applicationId); @@ -382,7 +382,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { PreparedStatement stmt = null; ResultSet rs = null; try { - conn = this.getConnection(); + conn = this.getDBConnection(); String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, applicationId); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index e260e1231ec..e3e5bf08794 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -116,24 +116,22 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public void deleteApplication(String uuid) throws ApplicationManagementException { - try { - ConnectionManagerUtil.openConnection(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); int appId = applicationDAO.getApplicationId(uuid); - ConnectionManagerUtil.beginTransaction(); + ConnectionManagerUtil.beginDBTransaction(); applicationDAO.deleteTags(appId); applicationDAO.deleteProperties(appId); applicationDAO.deleteApplication(uuid); - ConnectionManagerUtil.commitTransaction(); + ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Failed to delete application: " + uuid; throw new ApplicationManagementException(msg, e); } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index 0a99a14e476..d663a53c0ae 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -41,10 +41,118 @@ public class ConnectionManagerUtil { private static ThreadLocal currentTxState = new ThreadLocal<>(); private static DataSource dataSource; + public static Connection getDBConnection() throws DBConnectionException { + Connection conn = currentConnection.get(); + if (conn == null) { + try { + conn = dataSource.getConnection(); + currentConnection.set(conn); + } catch (SQLException e) { + throw new DBConnectionException("Failed to get database connection.", e); + } + } + return conn; + } + + public static void beginDBTransaction() throws TransactionManagementException, DBConnectionException { + Connection conn = currentConnection.get(); + if (conn == null) { + conn = getDBConnection(); + } + + if (inTransaction(conn)) { + throw new IllegalTransactionStateException("Transaction has already been started."); + } + + try { + conn.setAutoCommit(false); + } catch (SQLException e) { + throw new TransactionManagementException("Error occurred while starting a database transaction.", e); + } + } + + public static void endDBTransaction() throws TransactionManagementException, DBConnectionException { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("Database connection is not active."); + } + + if (!inTransaction(conn)) { + throw new IllegalTransactionStateException("Transaction has not been started."); + } + + try { + conn.setAutoCommit(true); + } catch (SQLException e) { + throw new TransactionManagementException("Error occurred while ending database transaction.", e); + } + } + + public static void commitDBTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("Database connection is not active."); + } + + if (!inTransaction(conn)) { + throw new IllegalTransactionStateException("Transaction has not been started."); + } + + try { + conn.commit(); + } catch (SQLException e) { + log.error("Error occurred while committing the transaction", e); + } + } + + public static void rollbackDBTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("Database connection is not active."); + } + + if (!inTransaction(conn)) { + throw new IllegalTransactionStateException("Transaction has not been started."); + } + + try { + conn.rollback(); + } catch (SQLException e) { + log.warn("Error occurred while roll-backing the transaction", e); + } + } + + public static void closeDBConnection() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("Database connection is not active."); + } + try { + conn.close(); + } catch (SQLException e) { + log.error("Error occurred while closing the connection", e); + } + currentConnection.remove(); + } + + private static boolean inTransaction(Connection conn) { + boolean inTransaction = true; + try { + if (conn.getAutoCommit()) { + inTransaction = false; + } + } catch (SQLException e) { + throw new IllegalTransactionStateException("Failed to get transaction state."); + } + return inTransaction; + } + + @Deprecated public static ThreadLocal getCurrentConnection() { return currentConnection; } + @Deprecated public static Connection openConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn != null) { @@ -63,6 +171,7 @@ public class ConnectionManagerUtil { return conn; } + @Deprecated public static Connection getConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) { @@ -73,6 +182,7 @@ public class ConnectionManagerUtil { return conn; } + @Deprecated public static void beginTransaction() throws TransactionManagementException, DBConnectionException { Connection conn = currentConnection.get(); if (conn != null) { @@ -102,6 +212,7 @@ public class ConnectionManagerUtil { currentTxState.set(TxState.CONNECTION_BORROWED); } + @Deprecated public static void commitTransaction() { Connection conn = currentConnection.get(); if (conn == null) { @@ -116,6 +227,7 @@ public class ConnectionManagerUtil { } } + @Deprecated public static void rollbackTransaction() { Connection conn = currentConnection.get(); if (conn == null) { @@ -130,6 +242,7 @@ public class ConnectionManagerUtil { } } + @Deprecated public static void closeConnection() { if(currentTxState != null) { TxState txState = currentTxState.get();