From a8910c851e616843d9489895755955bf06ea51fd Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Thu, 29 Dec 2011 16:20:13 +0400 Subject: Fix PurgeUtils createQuery() and createNativeQuery() should be invoked as many times as commit(), because it starts new transaction --- .../sonar/plugins/dbcleaner/api/PurgeUtils.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'plugins/sonar-dbcleaner-plugin/src/main') diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java index 390f4f0736f..37747560318 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeUtils.java @@ -26,6 +26,8 @@ import org.sonar.api.database.model.*; import org.sonar.api.design.DependencyDto; import org.sonar.api.utils.TimeProfiler; +import com.google.common.annotations.VisibleForTesting; + import javax.persistence.Query; import java.util.List; @@ -141,27 +143,32 @@ public final class PurgeUtils { if (ids == null || ids.isEmpty()) { return; } - executeQuery(session, description, ids, session.createQuery(hql)); + TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description); + int index = 0; + while (index < ids.size()) { + List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS)); + Query query = session.createQuery(hql); + query.setParameter("ids", paginedSids); + query.executeUpdate(); + index += MAX_IN_ELEMENTS; + session.commit(); + } + profiler.stop(); } /** * @since 2.13 */ - private static void executeNativeQuery(DatabaseSession session, String description, List ids, String sql) { + @VisibleForTesting + static void executeNativeQuery(DatabaseSession session, String description, List ids, String sql) { if (ids == null || ids.isEmpty()) { return; } - executeQuery(session, description, ids, session.createNativeQuery(sql)); - } - - /** - * @since 2.13 - */ - private static void executeQuery(DatabaseSession session, String description, List ids, Query query) { TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description); int index = 0; while (index < ids.size()) { List paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS)); + Query query = session.createNativeQuery(sql); query.setParameter("ids", paginedSids); query.executeUpdate(); index += MAX_IN_ELEMENTS; -- cgit v1.2.3