From c3f69fdc54fdeb415560fcc6a120c5eb382f7ef0 Mon Sep 17 00:00:00 2001 From: mposolda Date: Mon, 11 Jan 2016 15:31:45 +0100 Subject: [PATCH] Fix MySQL - fixing transactions during bootstrap --- .../exportimport/ExportImportManager.java | 13 ++--- .../services/managers/ApplianceBootstrap.java | 52 ++++++++----------- .../resources/KeycloakApplication.java | 30 +++++++---- 3 files changed, 48 insertions(+), 47 deletions(-) diff --git a/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java b/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java index 4f94b52b79..a373de40ff 100644 --- a/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java +++ b/services/src/main/java/org/keycloak/exportimport/ExportImportManager.java @@ -3,6 +3,7 @@ package org.keycloak.exportimport; import org.jboss.logging.Logger; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.KeycloakSessionFactory; import java.io.IOException; @@ -13,7 +14,7 @@ public class ExportImportManager { private static final Logger logger = Logger.getLogger(ExportImportManager.class); - private KeycloakSession session; + private KeycloakSessionFactory sessionFactory; private final String realmName; @@ -21,7 +22,7 @@ public class ExportImportManager { private ImportProvider importProvider; public ExportImportManager(KeycloakSession session) { - this.session = session; + this.sessionFactory = session.getKeycloakSessionFactory(); realmName = ExportImportConfig.getRealmName(); @@ -65,10 +66,10 @@ public class ExportImportManager { Strategy strategy = ExportImportConfig.getStrategy(); if (realmName == null) { logger.infof("Full model import requested. Strategy: %s", strategy.toString()); - importProvider.importModel(session.getKeycloakSessionFactory(), strategy); + importProvider.importModel(sessionFactory, strategy); } else { logger.infof("Import of realm '%s' requested. Strategy: %s", realmName, strategy.toString()); - importProvider.importRealm(session.getKeycloakSessionFactory(), realmName, strategy); + importProvider.importRealm(sessionFactory, realmName, strategy); } logger.info("Import finished successfully"); } catch (IOException e) { @@ -80,10 +81,10 @@ public class ExportImportManager { try { if (realmName == null) { logger.info("Full model export requested"); - exportProvider.exportModel(session.getKeycloakSessionFactory()); + exportProvider.exportModel(sessionFactory); } else { logger.infof("Export of realm '%s' requested", realmName); - exportProvider.exportRealm(session.getKeycloakSessionFactory(), realmName); + exportProvider.exportRealm(sessionFactory, realmName); } logger.info("Export finished successfully"); } catch (IOException e) { diff --git a/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java b/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java index 829dd6c9ac..10a260c790 100755 --- a/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java +++ b/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java @@ -39,37 +39,29 @@ public class ApplianceBootstrap { throw new IllegalStateException("Can't create default realm as realms already exists"); } - KeycloakSession session = this.session.getKeycloakSessionFactory().create(); - try { - session.getTransaction().begin(); - String adminRealmName = Config.getAdminRealm(); - logger.info("Initializing " + adminRealmName + " realm"); + String adminRealmName = Config.getAdminRealm(); + logger.info("Initializing " + adminRealmName + " realm"); - RealmManager manager = new RealmManager(session); - manager.setContextPath(contextPath); - RealmModel realm = manager.createRealm(adminRealmName, adminRealmName); - realm.setName(adminRealmName); - realm.setDisplayName(Version.NAME); - realm.setDisplayNameHtml(Version.NAME_HTML); - realm.setEnabled(true); - realm.addRequiredCredential(CredentialRepresentation.PASSWORD); - realm.setSsoSessionIdleTimeout(1800); - realm.setAccessTokenLifespan(60); - realm.setAccessTokenLifespanForImplicitFlow(Constants.DEFAULT_ACCESS_TOKEN_LIFESPAN_FOR_IMPLICIT_FLOW_TIMEOUT); - realm.setSsoSessionMaxLifespan(36000); - realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT); - realm.setAccessCodeLifespan(60); - realm.setAccessCodeLifespanUserAction(300); - realm.setAccessCodeLifespanLogin(1800); - realm.setSslRequired(SslRequired.EXTERNAL); - realm.setRegistrationAllowed(false); - realm.setRegistrationEmailAsUsername(false); - KeycloakModelUtils.generateRealmKeys(realm); - - session.getTransaction().commit(); - } finally { - session.close(); - } + RealmManager manager = new RealmManager(session); + manager.setContextPath(contextPath); + RealmModel realm = manager.createRealm(adminRealmName, adminRealmName); + realm.setName(adminRealmName); + realm.setDisplayName(Version.NAME); + realm.setDisplayNameHtml(Version.NAME_HTML); + realm.setEnabled(true); + realm.addRequiredCredential(CredentialRepresentation.PASSWORD); + realm.setSsoSessionIdleTimeout(1800); + realm.setAccessTokenLifespan(60); + realm.setAccessTokenLifespanForImplicitFlow(Constants.DEFAULT_ACCESS_TOKEN_LIFESPAN_FOR_IMPLICIT_FLOW_TIMEOUT); + realm.setSsoSessionMaxLifespan(36000); + realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT); + realm.setAccessCodeLifespan(60); + realm.setAccessCodeLifespanUserAction(300); + realm.setAccessCodeLifespanLogin(1800); + realm.setSslRequired(SslRequired.EXTERNAL); + realm.setRegistrationAllowed(false); + realm.setRegistrationEmailAsUsername(false); + KeycloakModelUtils.generateRealmKeys(realm); return true; } 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 88a6cc342c..4f4b992bc0 100755 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -83,11 +83,12 @@ public class KeycloakApplication extends Application { boolean bootstrapAdminUser = false; KeycloakSession session = sessionFactory.create(); + ExportImportManager exportImportManager; try { session.getTransaction().begin(); ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session); - ExportImportManager exportImportManager = new ExportImportManager(session); + exportImportManager = new ExportImportManager(session); boolean createMasterRealm = applianceBootstrap.isNewInstall(); if (exportImportManager.isRunImport() && exportImportManager.isImportMasterIncluded()) { @@ -97,20 +98,27 @@ public class KeycloakApplication extends Application { if (createMasterRealm) { applianceBootstrap.createMasterRealm(contextPath); } + session.getTransaction().commit(); + } finally { + session.close(); + } - if (exportImportManager.isRunImport()) { - exportImportManager.runImport(); - } else { - importRealms(); - } + if (exportImportManager.isRunImport()) { + exportImportManager.runImport(); + } else { + importRealms(); + } - importAddUser(); + importAddUser(); - if (exportImportManager.isRunExport()) { - exportImportManager.runExport(); - } + if (exportImportManager.isRunExport()) { + exportImportManager.runExport(); + } - bootstrapAdminUser = applianceBootstrap.isNoMasterUser(); + session = sessionFactory.create(); + try { + session.getTransaction().begin(); + bootstrapAdminUser = new ApplianceBootstrap(session).isNoMasterUser(); session.getTransaction().commit(); } finally {