Fix MySQL - fixing transactions during bootstrap

This commit is contained in:
mposolda 2016-01-11 15:31:45 +01:00
parent 802488cea0
commit c3f69fdc54
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,9 +39,6 @@ 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");
@ -66,11 +63,6 @@ public class ApplianceBootstrap {
realm.setRegistrationEmailAsUsername(false);
KeycloakModelUtils.generateRealmKeys(realm);
session.getTransaction().commit();
} finally {
session.close();
}
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,6 +98,10 @@ public class KeycloakApplication extends Application {
if (createMasterRealm) {
applianceBootstrap.createMasterRealm(contextPath);
}
session.getTransaction().commit();
} finally {
session.close();
}
if (exportImportManager.isRunImport()) {
exportImportManager.runImport();
@ -110,7 +115,10 @@ public class KeycloakApplication extends Application {
exportImportManager.runExport();
}
bootstrapAdminUser = applianceBootstrap.isNoMasterUser();
session = sessionFactory.create();
try {
session.getTransaction().begin();
bootstrapAdminUser = new ApplianceBootstrap(session).isNoMasterUser();
session.getTransaction().commit();
} finally {