*/
package org.sonar.plugins.dbcleaner.runner;
+import org.hibernate.HibernateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.PostJob;
private void executeDeprecatedPurges(DefaultPurgeContext context) {
TimeProfiler profiler = new TimeProfiler();
for (org.sonar.api.batch.Purge purge : deprecatedPurges) {
- profiler.start("Purge " + purge.getClass().getName());
- purge.purge(context);
- profiler.stop();
+ try {
+ profiler.start("Purge " + purge.getClass().getName());
+ purge.purge(context);
+ session.commit();// force hibernate to commit, so we're sure that the potential raised exception comes from this purge
+ profiler.stop();
+
+ } catch (javax.persistence.PersistenceException e) {
+ // Temporary workaround for MySQL deadlocks. The exception must not fail the build
+ // See https://jira.codehaus.org/browse/SONAR-2961 and https://jira.codehaus.org/browse/SONAR-2190
+ LOG.warn("Fail to execute purge: " + purge, e);
+
+ } catch (HibernateException e) {
+ // Temporary workaround for MySQL deadlocks. The exception must not fail the build
+ // See https://jira.codehaus.org/browse/SONAR-2961 and https://jira.codehaus.org/browse/SONAR-2190
+ LOG.warn("Fail to execute purge: " + purge, e);
+ }
}
}
private void executePurges(DefaultPurgeContext context) {
TimeProfiler profiler = new TimeProfiler();
for (Purge purge : purges) {
- profiler.start("Purge " + purge.getClass().getName());
- purge.purge(context);
- profiler.stop();
+ try {
+ profiler.start("Purge " + purge.getClass().getName());
+ purge.purge(context);
+ session.commit(); // force hibernate to commit, so we're sure that the potential raised exception comes from this purge
+ profiler.stop();
+ } catch (javax.persistence.PersistenceException e) {
+ // Temporary workaround for MySQL deadlocks. The exception must not fail the build
+ // See https://jira.codehaus.org/browse/SONAR-2961 and https://jira.codehaus.org/browse/SONAR-2190
+ LOG.warn("Fail to execute purge: " + purge, e);
+
+ } catch (HibernateException e) {
+ // Temporary workaround for MySQL deadlocks. The exception must not fail the build
+ // See https://jira.codehaus.org/browse/SONAR-2961 and https://jira.codehaus.org/browse/SONAR-2190
+ LOG.warn("Fail to execute purge: " + purge, e);
+ }
}
}
private Snapshot getPreviousLastSnapshot() {
Query query = session.createQuery(
- "SELECT s FROM " + Snapshot.class.getSimpleName() + " s " +
- "WHERE s.status=:status AND s.resourceId=:resourceId AND s.createdAt<:date AND s.id <> :sid ORDER BY s.createdAt DESC");
+ "SELECT s FROM " + Snapshot.class.getSimpleName() + " s " +
+ "WHERE s.status=:status AND s.resourceId=:resourceId AND s.createdAt<:date AND s.id <> :sid ORDER BY s.createdAt DESC");
query.setParameter("status", Snapshot.STATUS_PROCESSED);
query.setParameter("resourceId", snapshot.getResourceId());
query.setParameter("date", snapshot.getCreatedAt());
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
+import org.mockito.Matchers;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.plugins.dbcleaner.api.Purge;
import org.sonar.plugins.dbcleaner.api.PurgeContext;
+import javax.persistence.PersistenceException;
+
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.argThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
public class PurgeRunnerTest extends AbstractDbUnitTestCase {
when(project.isRoot()).thenReturn(false);
assertFalse(PurgeRunner.shouldExecuteOn(project));
}
+
+ /**
+ * See https://jira.codehaus.org/browse/SONAR-2961
+ * Temporarily ignore MySQL deadlocks
+ */
+ @Test
+ public void shouldIgnoreDeadlocks() {
+ Purge purge = mock(Purge.class);
+ doThrow(new PersistenceException()).when(purge).purge((PurgeContext)anyObject());
+
+ PurgeRunner runner = new PurgeRunner(getSession(), new Project(""), new Snapshot(), new Purge[]{purge});
+ runner.purge();// must not raise any exceptions
+
+ verify(purge).purge((PurgeContext)anyObject());
+ }
}