Improve DB connection handling

merge-requests/74/head
lasanthaDLPDS 6 years ago
parent dc518dcca2
commit 6e25a0bc3e

@ -207,8 +207,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationList applicationList; ApplicationList applicationList;
List<Application> appList; List<Application> apps;
List<ApplicationRelease> applicationReleases; List<ApplicationRelease> releases;
filter = validateFilter(filter); filter = validateFilter(filter);
if (filter == null) { if (filter == null) {
@ -233,21 +233,21 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
applicationList = applicationDAO.getApplications(filter, tenantId); applicationList = applicationDAO.getApplications(filter, tenantId);
appList = applicationList.getApplications(); apps = applicationList.getApplications();
for ( Application app : appList){ for ( Application app : apps){
if (AppLifecycleState.REMOVED.toString().equals(app.getStatus())){ if (AppLifecycleState.REMOVED.toString().equals(app.getStatus())){
appList.remove(app); apps.remove(app);
} }
} }
applicationList.setApplications(appList); applicationList.setApplications(apps);
if (applicationList.getApplications() != null && !applicationList if (applicationList.getApplications() != null && !applicationList
.getApplications().isEmpty()) { .getApplications().isEmpty()) {
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationList = getRoleRestrictedApplicationList(applicationList, userName); applicationList = getRoleRestrictedApplicationList(applicationList, userName);
} }
for (Application application : applicationList.getApplications()) { for (Application application : applicationList.getApplications()) {
applicationReleases = getReleases(application, filter.getCurrentAppReleaseState()); releases = getReleases(application, filter.getCurrentAppReleaseState());
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(releases);
} }
} }
return applicationList; return applicationList;
@ -617,7 +617,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
this.applicationDAO.deleteApplication(applicationId); this.applicationDAO.deleteApplication(applicationId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while check whether current user has the permission to delete an application"; String msg = "Error occured while check whether current user has the permission to delete an application";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
@ -688,12 +687,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
application = this.applicationDAO.getApplicationById(applicationId, tenantId); application = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (application == null) { if (application == null) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException("Couldn't find an application for application ID: " + applicationId); throw new NotFoundException("Couldn't find an application for application ID: " + applicationId);
} }
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION) && !application
.getUnrestrictedRoles().isEmpty() && isRoleExists(application.getUnrestrictedRoles(), userName)) { .getUnrestrictedRoles().isEmpty() && isRoleExists(application.getUnrestrictedRoles(), userName)) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ForbiddenException( throw new ForbiddenException(
"You don't have permission for deleting application release. Application id: " + applicationId "You don't have permission for deleting application release. Application id: " + applicationId
+ " and release UUID: " + releaseUuid); + " and release UUID: " + releaseUuid);
@ -702,14 +699,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationRelease applicationRelease = this.applicationReleaseDAO ApplicationRelease applicationRelease = this.applicationReleaseDAO
.getReleaseByIds(applicationId, releaseUuid, tenantId); .getReleaseByIds(applicationId, releaseUuid, tenantId);
if (applicationRelease == null) { if (applicationRelease == null) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException("Couldn't find an application release for application ID: " + applicationId throw new NotFoundException("Couldn't find an application release for application ID: " + applicationId
+ " and release UUID: " + releaseUuid); + " and release UUID: " + releaseUuid);
} }
LifecycleState appLifecycleState = this.lifecycleStateDAO LifecycleState appLifecycleState = this.lifecycleStateDAO
.getLatestLifeCycleState(applicationId, releaseUuid); .getLatestLifeCycleState(applicationId, releaseUuid);
if (appLifecycleState == null) { if (appLifecycleState == null) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException( throw new NotFoundException(
"Couldn't find an lifecycle sate for application ID: " + applicationId + " and UUID: " "Couldn't find an lifecycle sate for application ID: " + applicationId + " and UUID: "
+ releaseUuid); + releaseUuid);
@ -744,7 +739,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Can't delete the application release, You have to move the " + "lifecycle state from " "Can't delete the application release, You have to move the " + "lifecycle state from "
+ currentState + " to acceptable " + "state"); + currentState + " to acceptable " + "state");
@ -761,10 +755,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
"Error occured when deleting application release for application ID of " + applicationId "Error occured when deleting application release for application ID of " + applicationId
+ " and application release UUID of " + releaseUuid, e); + " and application release UUID of " + releaseUuid, e);
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occured when checking permission for executing application release update. Application ID: " "Error occured when checking permission for executing application release update. Application ID: "
+ applicationId + " and Application UUID: " + releaseUuid); + applicationId + " and Application UUID: " + releaseUuid);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -877,7 +872,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
applicationRelease = this.applicationReleaseDAO.getReleaseByIds(appId, uuid, tenantId); applicationRelease = this.applicationReleaseDAO.getReleaseByIds(appId, uuid, tenantId);
if (applicationRelease == null) { if (applicationRelease == null) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException( throw new NotFoundException(
"Doesn't exist a application release for application ID: " + appId + "and application UUID: " "Doesn't exist a application release for application ID: " + appId + "and application UUID: "
+ uuid); + uuid);
@ -886,7 +880,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
.getLatestLifeCycleState(appId, applicationRelease.getUuid()); .getLatestLifeCycleState(appId, applicationRelease.getUuid());
if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState()) if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState())
|| AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) { || AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ForbiddenException("Can't Update the application release in PUBLISHED or DEPRECATED state. " throw new ForbiddenException("Can't Update the application release in PUBLISHED or DEPRECATED state. "
+ "Hence please demote the application and update the application release"); + "Hence please demote the application and update the application release");
} }
@ -954,12 +947,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
Application application = this.applicationDAO.getApplicationById(appId, tenantId); Application application = this.applicationDAO.getApplicationById(appId, tenantId);
if (application == null) { if (application == null) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException("Doesn't exist a application for the application ID: " + appId); throw new NotFoundException("Doesn't exist a application for the application ID: " + appId);
} }
applicationRelease = this.applicationReleaseDAO.getReleaseByIds(appId, uuid, tenantId); applicationRelease = this.applicationReleaseDAO.getReleaseByIds(appId, uuid, tenantId);
if (applicationRelease == null) { if (applicationRelease == null) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new NotFoundException( throw new NotFoundException(
"Doesn't exist a application release for application ID: " + appId + "and application UUID: " "Doesn't exist a application release for application ID: " + appId + "and application UUID: "
+ uuid); + uuid);

Loading…
Cancel
Save