]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6566 Unable to deliver notifications on MySQL
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 20 May 2015 11:47:28 +0000 (13:47 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 20 May 2015 12:17:10 +0000 (14:17 +0200)
server/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java
server/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java

index 58d52639dc0df29246520ed2af6bf14237ceb91f..e51a4512aaec01eaa515ba156bd6a7ea7de2de10 100644 (file)
@@ -23,6 +23,14 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.SetMultimap;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 import org.picocontainer.Startable;
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
@@ -34,17 +42,9 @@ import org.sonar.api.notifications.NotificationDispatcher;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.core.notification.DefaultNotificationManager;
+import org.sonar.jpa.session.DatabaseSessionFactory;
 import org.sonar.server.db.DbClient;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-
 /**
  * @since 2.10
  */
@@ -71,6 +71,7 @@ public class NotificationService implements ServerComponent, Startable {
 
   private final long delayInSeconds;
   private final long delayBeforeReportingStatusInSeconds;
+  private final DatabaseSessionFactory databaseSessionFactory;
   private final DefaultNotificationManager manager;
   private final List<NotificationDispatcher> dispatchers;
   private final DbClient dbClient;
@@ -81,7 +82,8 @@ public class NotificationService implements ServerComponent, Startable {
   /**
    * Constructor for {@link NotificationService}
    */
-  public NotificationService(Settings settings, DefaultNotificationManager manager, DbClient dbClient, NotificationDispatcher[] dispatchers) {
+  public NotificationService(Settings settings, DefaultNotificationManager manager, DbClient dbClient, DatabaseSessionFactory databaseSessionFactory, NotificationDispatcher[] dispatchers) {
+    this.databaseSessionFactory = databaseSessionFactory;
     this.delayInSeconds = settings.getLong(PROPERTY_DELAY);
     this.delayBeforeReportingStatusInSeconds = settings.getLong(PROPERTY_DELAY_BEFORE_REPORTING_STATUS);
     this.manager = manager;
@@ -92,8 +94,8 @@ public class NotificationService implements ServerComponent, Startable {
   /**
    * Default constructor when no dispatchers.
    */
-  public NotificationService(Settings settings, DefaultNotificationManager manager, DbClient dbClient) {
-    this(settings, manager, dbClient, new NotificationDispatcher[0]);
+  public NotificationService(Settings settings, DefaultNotificationManager manager, DbClient dbClient, DatabaseSessionFactory databaseSessionFactory) {
+    this(settings, manager, dbClient, databaseSessionFactory, new NotificationDispatcher[0]);
   }
 
   @Override
@@ -106,6 +108,10 @@ public class NotificationService implements ServerComponent, Startable {
           processQueue();
         } catch (Exception e) {
           LOG.error("Error in NotificationService", e);
+        } finally {
+          // Free Hibernate session
+          // See https://jira.codehaus.org/browse/SONAR-6566
+          databaseSessionFactory.clear();
         }
       }
     }, 0, delayInSeconds, TimeUnit.SECONDS);
index 98f07a6048c2c6e6902acbf75f1efb679fb431c5..9ef0bf048991b453bef947bfb602afbe59b5b10e 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.api.notifications.NotificationChannel;
 import org.sonar.api.notifications.NotificationDispatcher;
 import org.sonar.core.notification.DefaultNotificationManager;
 import org.sonar.core.properties.PropertiesDao;
+import org.sonar.jpa.session.DatabaseSessionFactory;
 import org.sonar.server.db.DbClient;
 
 import java.util.Arrays;
@@ -69,7 +70,8 @@ public class NotificationServiceTest {
     Settings settings = new Settings().setProperty("sonar.notifications.delay", 1L);
 
     service = new NotificationService(settings, manager,
-      dbClient, new NotificationDispatcher[] {commentOnIssueAssignedToMe, commentOnIssueCreatedByMe, qualityGateChange});
+      dbClient, mock(DatabaseSessionFactory.class),
+      new NotificationDispatcher[] {commentOnIssueAssignedToMe, commentOnIssueCreatedByMe, qualityGateChange});
   }
 
   /**
@@ -205,7 +207,7 @@ public class NotificationServiceTest {
   public void getDispatchers_empty() {
     Settings settings = new Settings().setProperty("sonar.notifications.delay", 1L);
 
-    service = new NotificationService(settings, manager, dbClient);
+    service = new NotificationService(settings, manager, dbClient, mock(DatabaseSessionFactory.class));
     assertThat(service.getDispatchers()).hasSize(0);
   }