diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-16 14:37:26 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-16 14:37:26 +0000 |
commit | 4b756e42a163a02f2bf29dca930295cfd3c5f0ea (patch) | |
tree | e84f9f32ee08d33ff9c844d5b857bca08ad75545 | |
parent | ca75da099a891bdc4fd4e2249deaedb0da2effc8 (diff) | |
download | sonarqube-4b756e42a163a02f2bf29dca930295cfd3c5f0ea.tar.gz sonarqube-4b756e42a163a02f2bf29dca930295cfd3c5f0ea.zip |
fix memory leak : hibernate session is not correctly cleared with batch processing mode
3 files changed, 8 insertions, 18 deletions
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java index 8390dd8fd6b..5a350117670 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java +++ b/sonar-core/src/main/java/org/sonar/jpa/session/JpaDatabaseSession.java @@ -55,7 +55,6 @@ public class JpaDatabaseSession extends DatabaseSession { public void stop() { commit(); if (entityManager != null && entityManager.isOpen()) { - entityManager.clear(); entityManager.close(); entityManager = null; } @@ -69,9 +68,10 @@ public class JpaDatabaseSession extends DatabaseSession { } else { entityManager.getTransaction().commit(); } + entityManager.clear(); + index = 0; } inTransaction = false; - index = 0; } } @@ -79,7 +79,6 @@ public class JpaDatabaseSession extends DatabaseSession { if (entityManager != null && inTransaction) { entityManager.getTransaction().rollback(); inTransaction = false; - index = 0; } } @@ -110,7 +109,7 @@ public class JpaDatabaseSession extends DatabaseSession { private void internalSave(Object model, boolean flushIfNeeded) { entityManager.persist(model); if (flushIfNeeded && (++index % BATCH_SIZE == 0)) { - flush(); + commit(); } } @@ -123,7 +122,7 @@ public class JpaDatabaseSession extends DatabaseSession { startTransaction(); entityManager.remove(model); if (++index % BATCH_SIZE == 0) { - flush(); + commit(); } } @@ -144,10 +143,6 @@ public class JpaDatabaseSession extends DatabaseSession { } } - public void flush() { - entityManager.flush(); - entityManager.clear(); - } public Query createQuery(String hql) { startTransaction(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java index 144e813f370..99af8cfc275 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java @@ -68,6 +68,7 @@ public final class RegisterProvidedProfiles { DatabaseSession session = sessionFactory.getSession(); cleanProvidedProfiles(profiles, session); saveProvidedProfiles(profiles, session); + session.commit(); profiler.stop(); } @@ -108,7 +109,6 @@ public final class RegisterProvidedProfiles { existingProfile.setActiveRules(new ArrayList<ActiveRule>()); session.saveWithoutFlush(existingProfile); } - session.commit(); } profiler.stop(); } @@ -130,7 +130,6 @@ public final class RegisterProvidedProfiles { } session.saveWithoutFlush(persistedProfile); - session.commit(); profiler.stop(); } diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java index c222152667c..6f01506b6bc 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java @@ -64,6 +64,8 @@ public final class RegisterRules { profiler.start("Disable deprecated active rules"); deleteDisabledActiveRules(session); profiler.stop(); + + session.commit(); } private void disableDeprecatedUserRules(DatabaseSession session) { @@ -81,12 +83,11 @@ public final class RegisterRules { rule.setEnabled(false); session.saveWithoutFlush(rule); } - session.commit(); + } private void disableAllRules(DatabaseSession session) { session.createQuery("UPDATE " + Rule.class.getSimpleName() + " SET enabled=false WHERE parent IS NULL").executeUpdate(); - session.commit(); } private void registerRepository(RuleRepository repository, DatabaseSession session) { @@ -107,7 +108,6 @@ public final class RegisterRules { } saveNewRules(rulesByKey.values(), session); - session.commit(); } private void deleteDisabledActiveRules(DatabaseSession session) { @@ -118,9 +118,6 @@ public final class RegisterRules { for (ActiveRule deprecatedActiveRule : deprecatedActiveRules) { session.removeWithoutFlush(deprecatedActiveRule); } - if (!deprecatedActiveRules.isEmpty()) { - session.commit(); - } } private void updateRule(Rule persistedRule, Rule rule, DatabaseSession session) { @@ -173,6 +170,5 @@ public final class RegisterRules { rule.setEnabled(true); session.saveWithoutFlush(rule); } - session.commit(); } } |