]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6749 add some overrides which take a DbSession argument 553/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 1 Oct 2015 09:05:36 +0000 (11:05 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 1 Oct 2015 11:44:46 +0000 (13:44 +0200)
this avoid using multiple JDBC connection in parallele in views

server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java
server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java

index 3f4edbe5cc78bdcaba7bae067324c8573498dacb..1d342e65da18cf3b6bfdb5bcb3aebb67b50a62e5 100644 (file)
@@ -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<ComponentDto> 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<ComponentDto> 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<String> componentUuids(@Nullable Collection<String> componentKeys) {
index 93b31a6e84ff662d5fd126897666043fa2341ca6..5e18899fc336cf5420f8752bedea37af09ef9f36 100644 (file)
@@ -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
    */