From 732256eceb8ed9858a4f066a610a4e0eef0701ba Mon Sep 17 00:00:00 2001 From: mposolda Date: Mon, 1 Feb 2016 18:13:15 +0100 Subject: [PATCH] KEYCLOAK-2235 Fix transaction leaks --- .../models/UserFederationManager.java | 27 +++++++------- .../resources/KeycloakApplication.java | 35 +++++++++++-------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/server-spi/src/main/java/org/keycloak/models/UserFederationManager.java b/server-spi/src/main/java/org/keycloak/models/UserFederationManager.java index 3169ea4605..c58278b672 100755 --- a/server-spi/src/main/java/org/keycloak/models/UserFederationManager.java +++ b/server-spi/src/main/java/org/keycloak/models/UserFederationManager.java @@ -1,6 +1,7 @@ package org.keycloak.models; import org.jboss.logging.Logger; +import org.keycloak.models.utils.KeycloakModelUtils; import java.util.ArrayList; import java.util.Arrays; @@ -104,19 +105,19 @@ public class UserFederationManager implements UserProvider { } - protected void deleteInvalidUser(RealmModel realm, UserModel user) { - KeycloakSession tx = session.getKeycloakSessionFactory().create(); - try { - tx.getTransaction().begin(); - RealmModel realmModel = tx.realms().getRealm(realm.getId()); - if (realmModel == null) return; - UserModel deletedUser = tx.userStorage().getUserById(user.getId(), realmModel); - tx.userStorage().removeUser(realmModel, deletedUser); - logger.debugf("Removed invalid user '%s'", user.getUsername()); - tx.getTransaction().commit(); - } finally { - tx.close(); - } + protected void deleteInvalidUser(final RealmModel realm, final UserModel user) { + KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), new KeycloakSessionTask() { + + @Override + public void run(KeycloakSession session) { + RealmModel realmModel = session.realms().getRealm(realm.getId()); + if (realmModel == null) return; + UserModel deletedUser = session.userStorage().getUserById(user.getId(), realmModel); + session.userStorage().removeUser(realmModel, deletedUser); + logger.debugf("Removed invalid user '%s'", user.getUsername()); + } + + }); } diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java index 38cc097367..ff99c0b29c 100644 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -258,28 +258,33 @@ public class KeycloakApplication extends Application { public void importRealm(RealmRepresentation rep, String from) { KeycloakSession session = sessionFactory.create(); + boolean exists = false; try { session.getTransaction().begin(); - RealmManager manager = new RealmManager(session); - manager.setContextPath(getContextPath()); - - if (rep.getId() != null && manager.getRealm(rep.getId()) != null) { - logger.realmExists(rep.getRealm(), from); - return; - } - - if (manager.getRealmByName(rep.getRealm()) != null) { - logger.realmExists(rep.getRealm(), from); - return; - } try { - RealmModel realm = manager.importRealm(rep); + RealmManager manager = new RealmManager(session); + manager.setContextPath(getContextPath()); + + if (rep.getId() != null && manager.getRealm(rep.getId()) != null) { + logger.realmExists(rep.getRealm(), from); + exists = true; + } + + if (manager.getRealmByName(rep.getRealm()) != null) { + logger.realmExists(rep.getRealm(), from); + exists = true; + } + if (!exists) { + RealmModel realm = manager.importRealm(rep); + logger.importedRealm(realm.getName(), from); + } session.getTransaction().commit(); - logger.importedRealm(realm.getName(), from); } catch (Throwable t) { session.getTransaction().rollback(); - logger.unableToImportRealm(t, rep.getRealm(), from); + if (!exists) { + logger.unableToImportRealm(t, rep.getRealm(), from); + } } } finally { session.close();