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.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) {

View file

@ -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;
}

View file

@ -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 {