]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6315 use batch SQL session to insert change log
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 4 May 2017 14:20:50 +0000 (16:20 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 12 May 2017 09:18:19 +0000 (11:18 +0200)
in MassRegisterQualityProfiles

server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileInsert.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/DefinedQProfileInsertImpl.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/MassRegisterQualityProfiles.java

index 0eafd84ad6f3d6a4fd93c67a692f10c165b4062d..b689a6a70b68850d626005ffa6f16fe79a9cbf26 100644 (file)
@@ -24,5 +24,5 @@ import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
 
 public interface DefinedQProfileInsert {
-  void create(DbSession session, DefinedQProfile definedQProfile, OrganizationDto organization, List<ActiveRuleChange> changes);
+  void create(DbSession session, DbSession batchSession, DefinedQProfile definedQProfile, OrganizationDto organization, List<ActiveRuleChange> changes);
 }
index e52eda88b06115af63a9db03414a29806f68e53b..82ba53a02e43eb92b9e410d45b4ab6753ad62f11 100644 (file)
@@ -72,11 +72,10 @@ public class DefinedQProfileInsertImpl implements DefinedQProfileInsert {
   }
 
   @Override
-  public void create(DbSession session, DefinedQProfile definedQProfile, OrganizationDto organization, List<ActiveRuleChange> changes) {
-    initRuleRepository(session);
+  public void create(DbSession session, DbSession batchSession, DefinedQProfile definedQProfile, OrganizationDto organization, List<ActiveRuleChange> changes) {
+    initRuleRepository(batchSession);
 
-    checkArgument(definedQProfile.getParentQProfileName() == null,
-      "Inheritance of Quality Profiles is not supported yet");
+    checkArgument(definedQProfile.getParentQProfileName() == null, "Inheritance of Quality Profiles is not supported yet");
 
     Date now = new Date(system2.now());
     QualityProfileDto profileDto = insertQualityProfile(session, definedQProfile, organization, now);
@@ -86,7 +85,7 @@ public class DefinedQProfileInsertImpl implements DefinedQProfileInsert {
       .map(activeRule -> insertActiveRule(session, profileDto, activeRule, now.getTime()))
       .collect(MoreCollectors.toList());
 
-    localChanges.forEach(change -> dbClient.qProfileChangeDao().insert(session, change.toDto(null)));
+    localChanges.forEach(change -> dbClient.qProfileChangeDao().insert(batchSession, change.toDto(null)));
 
     insertTemplate(session, definedQProfile, organization);
 
index 6e2a2e769537648554b2b2c66e421772c48cc497..6cdd4a91a3b0b9ff524ff905fbc8c83f854c7a95 100644 (file)
@@ -75,26 +75,27 @@ public class MassRegisterQualityProfiles {
       return;
     }
 
-    try (DbSession session = dbClient.openSession(false)) {
+    try (DbSession session = dbClient.openSession(false);
+      DbSession batchSession = dbClient.openSession(true)) {
       List<ActiveRuleChange> changes = new ArrayList<>();
       definedQProfileRepository.getQProfilesByLanguage()
-        .forEach((key, value) -> registerPerLanguage(session, value, changes));
+        .forEach((key, value) -> registerPerLanguage(session, batchSession, value, changes));
       activeRuleIndexer.index(changes);
       profiler.stopDebug();
     }
   }
 
-  private void registerPerLanguage(DbSession session, List<DefinedQProfile> qualityProfiles, List<ActiveRuleChange> changes) {
-    qualityProfiles.forEach(qp -> registerPerQualityProfile(session, qp, changes));
+  private void registerPerLanguage(DbSession session, DbSession batchSession, List<DefinedQProfile> qualityProfiles, List<ActiveRuleChange> changes) {
+    qualityProfiles.forEach(qp -> registerPerQualityProfile(session, batchSession, qp, changes));
   }
 
-  private void registerPerQualityProfile(DbSession session, DefinedQProfile qualityProfile, List<ActiveRuleChange> changes) {
+  private void registerPerQualityProfile(DbSession session, DbSession batchSession, DefinedQProfile qualityProfile, List<ActiveRuleChange> changes) {
     LOGGER.info("Register profile {}", qualityProfile.getQProfileName());
 
     Profiler profiler = Profiler.create(Loggers.get(getClass()));
     List<OrganizationDto> organizationDtos;
     while (!(organizationDtos = getOrganizationsWithoutQP(session, qualityProfile)).isEmpty()) {
-      organizationDtos.forEach(organization -> registerPerQualityProfileAndOrganization(session, qualityProfile, organization, changes, profiler));
+      organizationDtos.forEach(organization -> registerPerQualityProfileAndOrganization(session, batchSession, qualityProfile, organization, changes, profiler));
     }
   }
 
@@ -103,13 +104,14 @@ public class MassRegisterQualityProfiles {
       qualityProfile.getLoadedTemplateType(), PROCESSED_ORGANIZATIONS_BATCH_SIZE);
   }
 
-  private void registerPerQualityProfileAndOrganization(DbSession session,
+  private void registerPerQualityProfileAndOrganization(DbSession session, DbSession batchSession,
     DefinedQProfile definedQProfile, OrganizationDto organization, List<ActiveRuleChange> changes, Profiler profiler) {
     profiler.start();
 
-    definedQProfileInsert.create(session, definedQProfile, organization, changes);
+    definedQProfileInsert.create(session, batchSession, definedQProfile, organization, changes);
 
     session.commit();
+    batchSession.commit();
 
     profiler.stopDebug(format("Register profile %s for organization %s", definedQProfile.getQProfileName(), organization.getKey()));
   }