Search in sources :

Example 1 with JtaTransactionManagerLookup

use of org.keycloak.transaction.JtaTransactionManagerLookup in project keycloak by keycloak.

the class QuarkusJpaConnectionProviderFactory method createMasterRealm.

private ExportImportManager createMasterRealm(ExportImportManager exportImportManager) {
    logger.debug("bootstrap");
    KeycloakSession session = factory.create();
    try {
        session.getTransactionManager().begin();
        JtaTransactionManagerLookup lookup = (JtaTransactionManagerLookup) factory.getProviderFactory(JtaTransactionManagerLookup.class);
        if (lookup != null) {
            if (lookup.getTransactionManager() != null) {
                try {
                    Transaction transaction = lookup.getTransactionManager().getTransaction();
                    logger.debugv("bootstrap current transaction? {0}", transaction != null);
                    if (transaction != null) {
                        logger.debugv("bootstrap current transaction status? {0}", transaction.getStatus());
                    }
                } catch (SystemException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
        boolean createMasterRealm = applianceBootstrap.isNewInstall();
        if (exportImportManager.isRunImport() && exportImportManager.isImportMasterIncluded()) {
            createMasterRealm = false;
        }
        if (createMasterRealm) {
            applianceBootstrap.createMasterRealm();
        }
        session.getTransactionManager().commit();
    } catch (RuntimeException re) {
        if (session.getTransactionManager().isActive()) {
            session.getTransactionManager().rollback();
        }
        throw re;
    } finally {
        session.close();
    }
    if (exportImportManager.isRunImport()) {
        exportImportManager.runImport();
        Quarkus.asyncExit();
    } else {
        importRealms();
    }
    importAddUser();
    return exportImportManager;
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) JtaTransactionManagerLookup(org.keycloak.transaction.JtaTransactionManagerLookup) Transaction(javax.transaction.Transaction) KeycloakModelUtils.runJobInTransaction(org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction) SystemException(javax.transaction.SystemException)

Example 2 with JtaTransactionManagerLookup

use of org.keycloak.transaction.JtaTransactionManagerLookup in project keycloak by keycloak.

the class KeycloakApplication method bootstrap.

// Bootstrap master realm, import realms and create admin user.
protected ExportImportManager bootstrap() {
    ExportImportManager[] exportImportManager = new ExportImportManager[1];
    logger.debug("bootstrap");
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            // TODO what is the purpose of following piece of code? Leaving it as is for now.
            JtaTransactionManagerLookup lookup = (JtaTransactionManagerLookup) sessionFactory.getProviderFactory(JtaTransactionManagerLookup.class);
            if (lookup != null) {
                if (lookup.getTransactionManager() != null) {
                    try {
                        Transaction transaction = lookup.getTransactionManager().getTransaction();
                        logger.debugv("bootstrap current transaction? {0}", transaction != null);
                        if (transaction != null) {
                            logger.debugv("bootstrap current transaction status? {0}", transaction.getStatus());
                        }
                    } catch (SystemException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            // TODO up here ^^
            ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
            exportImportManager[0] = new ExportImportManager(session);
            boolean createMasterRealm = applianceBootstrap.isNewInstall();
            if (exportImportManager[0].isRunImport() && exportImportManager[0].isImportMasterIncluded()) {
                createMasterRealm = false;
            }
            if (createMasterRealm) {
                applianceBootstrap.createMasterRealm();
            }
        }
    });
    if (exportImportManager[0].isRunImport()) {
        exportImportManager[0].runImport();
    } else {
        importRealms();
    }
    importAddUser();
    return exportImportManager[0];
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) JtaTransactionManagerLookup(org.keycloak.transaction.JtaTransactionManagerLookup) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) ExportImportManager(org.keycloak.exportimport.ExportImportManager)

Example 3 with JtaTransactionManagerLookup

use of org.keycloak.transaction.JtaTransactionManagerLookup in project keycloak by keycloak.

the class KeycloakModelUtils method suspendJtaTransaction.

public static void suspendJtaTransaction(KeycloakSessionFactory factory, Runnable runnable) {
    JtaTransactionManagerLookup lookup = (JtaTransactionManagerLookup) factory.getProviderFactory(JtaTransactionManagerLookup.class);
    Transaction suspended = null;
    try {
        if (lookup != null) {
            if (lookup.getTransactionManager() != null) {
                try {
                    suspended = lookup.getTransactionManager().suspend();
                } catch (SystemException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        runnable.run();
    } finally {
        if (suspended != null) {
            try {
                lookup.getTransactionManager().resume(suspended);
            } catch (InvalidTransactionException | SystemException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : JtaTransactionManagerLookup(org.keycloak.transaction.JtaTransactionManagerLookup) KeycloakTransaction(org.keycloak.models.KeycloakTransaction) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) InvalidTransactionException(javax.transaction.InvalidTransactionException)

Example 4 with JtaTransactionManagerLookup

use of org.keycloak.transaction.JtaTransactionManagerLookup in project keycloak by keycloak.

the class DefaultKeycloakTransactionManager method begin.

@Override
public void begin() {
    if (active) {
        throw new IllegalStateException("Transaction already active");
    }
    completed = false;
    if (jtaPolicy == JTAPolicy.REQUIRES_NEW) {
        JtaTransactionManagerLookup jtaLookup = session.getProvider(JtaTransactionManagerLookup.class);
        if (jtaLookup != null) {
            TransactionManager tm = jtaLookup.getTransactionManager();
            if (tm != null) {
                enlist(new JtaTransactionWrapper(session.getKeycloakSessionFactory(), tm));
            }
        }
    }
    for (KeycloakTransaction tx : transactions) {
        tx.begin();
    }
    active = true;
}
Also used : JtaTransactionManagerLookup(org.keycloak.transaction.JtaTransactionManagerLookup) KeycloakTransactionManager(org.keycloak.models.KeycloakTransactionManager) TransactionManager(javax.transaction.TransactionManager) JtaTransactionWrapper(org.keycloak.transaction.JtaTransactionWrapper) KeycloakTransaction(org.keycloak.models.KeycloakTransaction)

Aggregations

JtaTransactionManagerLookup (org.keycloak.transaction.JtaTransactionManagerLookup)4 SystemException (javax.transaction.SystemException)3 Transaction (javax.transaction.Transaction)3 KeycloakTransaction (org.keycloak.models.KeycloakTransaction)2 ApplianceBootstrap (org.keycloak.services.managers.ApplianceBootstrap)2 InvalidTransactionException (javax.transaction.InvalidTransactionException)1 TransactionManager (javax.transaction.TransactionManager)1 ExportImportManager (org.keycloak.exportimport.ExportImportManager)1 KeycloakSession (org.keycloak.models.KeycloakSession)1 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)1 KeycloakTransactionManager (org.keycloak.models.KeycloakTransactionManager)1 KeycloakModelUtils.runJobInTransaction (org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction)1 JtaTransactionWrapper (org.keycloak.transaction.JtaTransactionWrapper)1