KEYCLOAK-2235 Fix transaction leaks

This commit is contained in:
mposolda 2016-02-01 18:13:15 +01:00
parent 77a7b5a130
commit 732256eceb
2 changed files with 34 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package org.keycloak.models; package org.keycloak.models;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.models.utils.KeycloakModelUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -104,19 +105,19 @@ public class UserFederationManager implements UserProvider {
} }
protected void deleteInvalidUser(RealmModel realm, UserModel user) { protected void deleteInvalidUser(final RealmModel realm, final UserModel user) {
KeycloakSession tx = session.getKeycloakSessionFactory().create(); KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), new KeycloakSessionTask() {
try {
tx.getTransaction().begin(); @Override
RealmModel realmModel = tx.realms().getRealm(realm.getId()); public void run(KeycloakSession session) {
if (realmModel == null) return; RealmModel realmModel = session.realms().getRealm(realm.getId());
UserModel deletedUser = tx.userStorage().getUserById(user.getId(), realmModel); if (realmModel == null) return;
tx.userStorage().removeUser(realmModel, deletedUser); UserModel deletedUser = session.userStorage().getUserById(user.getId(), realmModel);
logger.debugf("Removed invalid user '%s'", user.getUsername()); session.userStorage().removeUser(realmModel, deletedUser);
tx.getTransaction().commit(); logger.debugf("Removed invalid user '%s'", user.getUsername());
} finally { }
tx.close();
} });
} }

View file

@ -258,28 +258,33 @@ public class KeycloakApplication extends Application {
public void importRealm(RealmRepresentation rep, String from) { public void importRealm(RealmRepresentation rep, String from) {
KeycloakSession session = sessionFactory.create(); KeycloakSession session = sessionFactory.create();
boolean exists = false;
try { try {
session.getTransaction().begin(); 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 { 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(); session.getTransaction().commit();
logger.importedRealm(realm.getName(), from);
} catch (Throwable t) { } catch (Throwable t) {
session.getTransaction().rollback(); session.getTransaction().rollback();
logger.unableToImportRealm(t, rep.getRealm(), from); if (!exists) {
logger.unableToImportRealm(t, rep.getRealm(), from);
}
} }
} finally { } finally {
session.close(); session.close();