From 8c21482c0f7164e03e70c05833c1bee162408831 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 1 Oct 2015 11:05:36 +0200 Subject: [PATCH] SONAR-6749 add some overrides which take a DbSession argument this avoid using multiple JDBC connection in parallele in views --- .../server/component/ComponentService.java | 83 ++++++++++--------- .../server/permission/PermissionService.java | 23 +++-- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java index 3f4edbe5cc7..1d342e65da1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java @@ -146,62 +146,63 @@ public class ComponentService { public ComponentDto create(NewComponent newComponent) { userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING); - ComponentDto project = createProject(newComponent); - removeDuplicatedProjects(project.getKey()); - return project; - } - private ComponentDto createProject(NewComponent newComponent) { DbSession session = dbClient.openSession(false); try { - checkKeyFormat(newComponent.qualifier(), newComponent.key()); - checkBranchFormat(newComponent.qualifier(), newComponent.branch()); - String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch()); - - ComponentDto existingComponent = getNullableByKey(keyWithBranch); - if (existingComponent != null) { - throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch)); - } - - String uuid = Uuids.create(); - ComponentDto component = new ComponentDto() - .setUuid(uuid) - .setModuleUuid(null) - .setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + uuid + ComponentDto.MODULE_UUID_PATH_SEP) - .setProjectUuid(uuid) - .setKey(keyWithBranch) - .setDeprecatedKey(keyWithBranch) - .setName(newComponent.name()) - .setLongName(newComponent.name()) - .setScope(Scopes.PROJECT) - .setQualifier(newComponent.qualifier()) - .setCreatedAt(new Date(system2.now())); - dbClient.componentDao().insert(session, component); - dbClient.componentIndexDao().indexResource(session, component.getId()); - session.commit(); - return component; + return create(session, newComponent); } finally { dbClient.closeSession(session); } } + public ComponentDto create(DbSession session, NewComponent newComponent) { + userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING); + ComponentDto project = createProject(session, newComponent); + removeDuplicatedProjects(session, project.getKey()); + return project; + } + + private ComponentDto createProject(DbSession session, NewComponent newComponent) { + checkKeyFormat(newComponent.qualifier(), newComponent.key()); + checkBranchFormat(newComponent.qualifier(), newComponent.branch()); + String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch()); + + ComponentDto existingComponent = getNullableByKey(keyWithBranch); + if (existingComponent != null) { + throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch)); + } + + String uuid = Uuids.create(); + ComponentDto component = new ComponentDto() + .setUuid(uuid) + .setModuleUuid(null) + .setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + uuid + ComponentDto.MODULE_UUID_PATH_SEP) + .setProjectUuid(uuid) + .setKey(keyWithBranch) + .setDeprecatedKey(keyWithBranch) + .setName(newComponent.name()) + .setLongName(newComponent.name()) + .setScope(Scopes.PROJECT) + .setQualifier(newComponent.qualifier()) + .setCreatedAt(new Date(system2.now())); + dbClient.componentDao().insert(session, component); + dbClient.componentIndexDao().indexResource(session, component.getId()); + session.commit(); + return component; + } + /** * On MySQL, as PROJECTS.KEE is not unique, if the same project is provisioned multiple times, then it will be duplicated in the database. * So, after creating a project, we commit, and we search in the db if their are some duplications and we remove them. * * SONAR-6332 */ - private void removeDuplicatedProjects(String projectKey) { - DbSession session = dbClient.openSession(false); - try { - List duplicated = dbClient.componentDao().selectComponentsHavingSameKeyOrderedById(session, projectKey); - for (int i = 1; i < duplicated.size(); i++) { - dbClient.componentDao().delete(session, duplicated.get(i).getId()); - } - session.commit(); - } finally { - dbClient.closeSession(session); + private void removeDuplicatedProjects(DbSession session, String projectKey) { + List duplicated = dbClient.componentDao().selectComponentsHavingSameKeyOrderedById(session, projectKey); + for (int i = 1; i < duplicated.size(); i++) { + dbClient.componentDao().delete(session, duplicated.get(i).getId()); } + session.commit(); } public Collection componentUuids(@Nullable Collection componentKeys) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java index 93b31a6e84f..5e18899fc33 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java @@ -62,21 +62,26 @@ public class PermissionService { public void applyDefaultPermissionTemplate(String componentKey) { DbSession session = dbClient.openSession(false); try { - ComponentDto component = componentFinder.getByKey(session, componentKey); - ResourceDto provisioned = dbClient.resourceDao().selectProvisionedProject(session, componentKey); - if (provisioned == null) { - checkProjectAdminPermission(componentKey); - } else { - userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING); - } - permissionRepository.grantDefaultRoles(session, component.getId(), component.qualifier()); - session.commit(); + applyDefaultPermissionTemplate(session, componentKey); } finally { session.close(); } indexProjectPermissions(); } + public void applyDefaultPermissionTemplate(DbSession session, String componentKey) { + ComponentDto component = componentFinder.getByKey(session, componentKey); + ResourceDto provisioned = dbClient.resourceDao().selectProvisionedProject(session, componentKey); + if (provisioned == null) { + checkProjectAdminPermission(componentKey); + } else { + userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING); + } + permissionRepository.grantDefaultRoles(session, component.getId(), component.qualifier()); + session.commit(); + indexProjectPermissions(); + } + /** * Important - this method checks caller permissions */ -- 2.39.5