Merge pull request #2005 from mposolda/master

Fix MySQL - fixing transactions during bootstrap
This commit is contained in:
Marek Posolda 2016-01-11 17:02:08 +01:00
commit 5424cd80a8
3 changed files with 48 additions and 47 deletions

View file

@ -3,6 +3,7 @@ package org.keycloak.exportimport;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import java.io.IOException; import java.io.IOException;
@ -13,7 +14,7 @@ public class ExportImportManager {
private static final Logger logger = Logger.getLogger(ExportImportManager.class); private static final Logger logger = Logger.getLogger(ExportImportManager.class);
private KeycloakSession session; private KeycloakSessionFactory sessionFactory;
private final String realmName; private final String realmName;
@ -21,7 +22,7 @@ public class ExportImportManager {
private ImportProvider importProvider; private ImportProvider importProvider;
public ExportImportManager(KeycloakSession session) { public ExportImportManager(KeycloakSession session) {
this.session = session; this.sessionFactory = session.getKeycloakSessionFactory();
realmName = ExportImportConfig.getRealmName(); realmName = ExportImportConfig.getRealmName();
@ -65,10 +66,10 @@ public class ExportImportManager {
Strategy strategy = ExportImportConfig.getStrategy(); Strategy strategy = ExportImportConfig.getStrategy();
if (realmName == null) { if (realmName == null) {
logger.infof("Full model import requested. Strategy: %s", strategy.toString()); logger.infof("Full model import requested. Strategy: %s", strategy.toString());
importProvider.importModel(session.getKeycloakSessionFactory(), strategy); importProvider.importModel(sessionFactory, strategy);
} else { } else {
logger.infof("Import of realm '%s' requested. Strategy: %s", realmName, strategy.toString()); 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"); logger.info("Import finished successfully");
} catch (IOException e) { } catch (IOException e) {
@ -80,10 +81,10 @@ public class ExportImportManager {
try { try {
if (realmName == null) { if (realmName == null) {
logger.info("Full model export requested"); logger.info("Full model export requested");
exportProvider.exportModel(session.getKeycloakSessionFactory()); exportProvider.exportModel(sessionFactory);
} else { } else {
logger.infof("Export of realm '%s' requested", realmName); logger.infof("Export of realm '%s' requested", realmName);
exportProvider.exportRealm(session.getKeycloakSessionFactory(), realmName); exportProvider.exportRealm(sessionFactory, realmName);
} }
logger.info("Export finished successfully"); logger.info("Export finished successfully");
} catch (IOException e) { } catch (IOException e) {

View file

@ -39,37 +39,29 @@ public class ApplianceBootstrap {
throw new IllegalStateException("Can't create default realm as realms already exists"); throw new IllegalStateException("Can't create default realm as realms already exists");
} }
KeycloakSession session = this.session.getKeycloakSessionFactory().create(); String adminRealmName = Config.getAdminRealm();
try { logger.info("Initializing " + adminRealmName + " realm");
session.getTransaction().begin();
String adminRealmName = Config.getAdminRealm();
logger.info("Initializing " + adminRealmName + " realm");
RealmManager manager = new RealmManager(session); RealmManager manager = new RealmManager(session);
manager.setContextPath(contextPath); manager.setContextPath(contextPath);
RealmModel realm = manager.createRealm(adminRealmName, adminRealmName); RealmModel realm = manager.createRealm(adminRealmName, adminRealmName);
realm.setName(adminRealmName); realm.setName(adminRealmName);
realm.setDisplayName(Version.NAME); realm.setDisplayName(Version.NAME);
realm.setDisplayNameHtml(Version.NAME_HTML); realm.setDisplayNameHtml(Version.NAME_HTML);
realm.setEnabled(true); realm.setEnabled(true);
realm.addRequiredCredential(CredentialRepresentation.PASSWORD); realm.addRequiredCredential(CredentialRepresentation.PASSWORD);
realm.setSsoSessionIdleTimeout(1800); realm.setSsoSessionIdleTimeout(1800);
realm.setAccessTokenLifespan(60); realm.setAccessTokenLifespan(60);
realm.setAccessTokenLifespanForImplicitFlow(Constants.DEFAULT_ACCESS_TOKEN_LIFESPAN_FOR_IMPLICIT_FLOW_TIMEOUT); realm.setAccessTokenLifespanForImplicitFlow(Constants.DEFAULT_ACCESS_TOKEN_LIFESPAN_FOR_IMPLICIT_FLOW_TIMEOUT);
realm.setSsoSessionMaxLifespan(36000); realm.setSsoSessionMaxLifespan(36000);
realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT); realm.setOfflineSessionIdleTimeout(Constants.DEFAULT_OFFLINE_SESSION_IDLE_TIMEOUT);
realm.setAccessCodeLifespan(60); realm.setAccessCodeLifespan(60);
realm.setAccessCodeLifespanUserAction(300); realm.setAccessCodeLifespanUserAction(300);
realm.setAccessCodeLifespanLogin(1800); realm.setAccessCodeLifespanLogin(1800);
realm.setSslRequired(SslRequired.EXTERNAL); realm.setSslRequired(SslRequired.EXTERNAL);
realm.setRegistrationAllowed(false); realm.setRegistrationAllowed(false);
realm.setRegistrationEmailAsUsername(false); realm.setRegistrationEmailAsUsername(false);
KeycloakModelUtils.generateRealmKeys(realm); KeycloakModelUtils.generateRealmKeys(realm);
session.getTransaction().commit();
} finally {
session.close();
}
return true; return true;
} }

View file

@ -83,11 +83,12 @@ public class KeycloakApplication extends Application {
boolean bootstrapAdminUser = false; boolean bootstrapAdminUser = false;
KeycloakSession session = sessionFactory.create(); KeycloakSession session = sessionFactory.create();
ExportImportManager exportImportManager;
try { try {
session.getTransaction().begin(); session.getTransaction().begin();
ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session); ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
ExportImportManager exportImportManager = new ExportImportManager(session); exportImportManager = new ExportImportManager(session);
boolean createMasterRealm = applianceBootstrap.isNewInstall(); boolean createMasterRealm = applianceBootstrap.isNewInstall();
if (exportImportManager.isRunImport() && exportImportManager.isImportMasterIncluded()) { if (exportImportManager.isRunImport() && exportImportManager.isImportMasterIncluded()) {
@ -97,20 +98,27 @@ public class KeycloakApplication extends Application {
if (createMasterRealm) { if (createMasterRealm) {
applianceBootstrap.createMasterRealm(contextPath); applianceBootstrap.createMasterRealm(contextPath);
} }
session.getTransaction().commit();
} finally {
session.close();
}
if (exportImportManager.isRunImport()) { if (exportImportManager.isRunImport()) {
exportImportManager.runImport(); exportImportManager.runImport();
} else { } else {
importRealms(); importRealms();
} }
importAddUser(); importAddUser();
if (exportImportManager.isRunExport()) { if (exportImportManager.isRunExport()) {
exportImportManager.runExport(); exportImportManager.runExport();
} }
bootstrapAdminUser = applianceBootstrap.isNoMasterUser(); session = sessionFactory.create();
try {
session.getTransaction().begin();
bootstrapAdminUser = new ApplianceBootstrap(session).isNoMasterUser();
session.getTransaction().commit(); session.getTransaction().commit();
} finally { } finally {