From 8c75b30378c5876c4d5ff0473f82417f19168eaf Mon Sep 17 00:00:00 2001 From: ashvini Date: Sat, 6 Apr 2024 06:47:03 +0530 Subject: [PATCH] Fix AutoCommit issue in Tenant Deletion Set autocommit to false Fix error message Fix data not deleting issue Fix error message --- .../application/GenericApplicationDAOImpl.java | 18 ++++++++++++------ .../GenericApplicationReleaseDAOImpl.java | 3 ++- .../GenericSPApplicationDAOImpl.java | 3 ++- .../GenericLifecycleStateDAOImpl.java | 3 ++- .../dao/impl/review/GenericReviewDAOImpl.java | 3 ++- .../GenericSubscriptionDAOImpl.java | 18 ++++++++++++------ .../visibility/GenericVisibilityDAOImpl.java | 3 ++- .../impl/vpp/GenericVppApplicationDAOImpl.java | 9 ++++++--- .../pom.xml | 1 + .../mgt/core/impl/TenantManagerImpl.java | 9 +++++---- 10 files changed, 46 insertions(+), 24 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 0d6970b70d5..7c71880fa6d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -1891,7 +1891,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } String sql = "DELETE FROM AP_APP_FAVOURITES " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1917,7 +1918,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } String sql = "DELETE FROM AP_APP_CATEGORY_MAPPING " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1943,7 +1945,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } String sql = "DELETE FROM AP_APP_CATEGORY " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1969,7 +1972,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } String sql = "DELETE FROM AP_APP_TAG_MAPPING " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1995,7 +1999,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } String sql = "DELETE FROM AP_APP_TAG " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -2021,7 +2026,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } String sql = "DELETE FROM AP_APP " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 9cebf37f415..70ad2f4e174 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -632,7 +632,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } String sql = "DELETE FROM AP_APP_RELEASE " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java index 0511602f654..0b4d14073b1 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/spapplication/GenericSPApplicationDAOImpl.java @@ -491,7 +491,8 @@ public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPAp } String sql = "DELETE FROM AP_IDENTITY_SERVER " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java index 77026bb04ac..c32afcd79a3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java @@ -121,7 +121,8 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } String sql = "DELETE FROM AP_APP_LIFECYCLE_STATE " + "WHERE TENANT_ID = ?"; - try ( Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java index 71323aa8687..e31db113ef1 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/review/GenericReviewDAOImpl.java @@ -610,7 +610,8 @@ public class GenericReviewDAOImpl extends AbstractDAOImpl implements ReviewDAO { } String sql = "DELETE FROM AP_APP_REVIEW " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index e8a030a01dd..79e7b76660b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1484,7 +1484,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } String sql = "DELETE FROM AP_APP_SUB_OP_MAPPING " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1509,7 +1510,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } String sql = "DELETE FROM AP_ROLE_SUBSCRIPTION " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1535,7 +1537,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } String sql = "DELETE FROM AP_USER_SUBSCRIPTION " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1561,7 +1564,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } String sql = "DELETE FROM AP_GROUP_SUBSCRIPTION " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -1587,7 +1591,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc String sql = "DELETE FROM AP_SCHEDULED_SUBSCRIPTION " + "WHERE APPLICATION_UUID IN " + "(SELECT UUID FROM AP_APP_RELEASE WHERE TENANT_ID = ?)"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeBatch(); @@ -1612,7 +1617,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } String sql = "DELETE FROM AP_DEVICE_SUBSCRIPTION " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java index 446a54c7060..1033af9bef4 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/visibility/GenericVisibilityDAOImpl.java @@ -186,7 +186,8 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil String sql = "DELETE " + "FROM AP_UNRESTRICTED_ROLE " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java index 4b618894b5f..8cc62b4dbc5 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/vpp/GenericVppApplicationDAOImpl.java @@ -514,7 +514,8 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } String sql = "DELETE FROM AP_ASSETS " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -539,7 +540,8 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } String sql = "DELETE FROM AP_VPP_USER " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); @@ -565,7 +567,8 @@ public class GenericVppApplicationDAOImpl extends AbstractDAOImpl implements Vp } String sql = "DELETE FROM AP_VPP_ASSOCIATION " + "WHERE TENANT_ID = ?"; - try (Connection conn = this.getDBConnection()) { + try { + Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); stmt.executeUpdate(); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml index 413d88d9496..7cb910b6d9c 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml @@ -66,6 +66,7 @@ io.entgra.device.mgt.core.device.mgt.common.roles.config, io.entgra.device.mgt.core.device.mgt.core.metadata.mgt, io.entgra.device.mgt.core.device.mgt.core.config, + io.entgra.device.mgt.core.device.mgt.core.dao.*, org.wso2.carbon.user.core.service, org.wso2.carbon.context diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java index cbac0013b4a..791723ebe31 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.tenant.mgt.core.impl; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; @@ -200,13 +201,13 @@ public class TenantManagerImpl implements TenantManager { tenantDao.deleteDeviceCertificateByTenantId(tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (SQLException e){ + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error accessing the database when trying to delete tenant info of '" + tenantId + "'"; + String msg = "Error deleting data of tenant of ID: '" + tenantId + "'"; log.error(msg); throw new TenantMgtException(msg, e); - } catch (DeviceManagementDAOException e) { - String msg = "Error deleting data of tenant of ID: '" + tenantId + "'"; + } catch (SQLException e) { + String msg = "Error accessing the database when trying to delete tenant info of '" + tenantId + "'"; log.error(msg); throw new TenantMgtException(msg, e); } finally {