Search in sources :

Example 1 with KeycloakTransactionManager

use of org.keycloak.models.KeycloakTransactionManager in project keycloak by keycloak.

the class AbstractRequestFilter method close.

protected void close(KeycloakSession session) {
    KeycloakTransactionManager tx = session.getTransactionManager();
    if (tx.isActive()) {
        if (tx.getRollbackOnly()) {
            tx.rollback();
        } else {
            tx.commit();
        }
    }
    session.close();
}
Also used : KeycloakTransactionManager(org.keycloak.models.KeycloakTransactionManager)

Example 2 with KeycloakTransactionManager

use of org.keycloak.models.KeycloakTransactionManager in project keycloak by keycloak.

the class AbstractRequestFilter method filter.

protected void filter(ClientConnection clientConnection, Consumer<KeycloakSession> next) {
    KeycloakSessionFactory sessionFactory = getSessionFactory();
    KeycloakSession session = sessionFactory.create();
    KeycloakTransactionManager tx = session.getTransactionManager();
    tx.begin();
    try {
        Resteasy.pushContext(ClientConnection.class, clientConnection);
        Resteasy.pushContext(KeycloakSession.class, session);
        next.accept(session);
    } catch (Exception e) {
        tx.setRollbackOnly();
        throw new RuntimeException(e);
    } finally {
        if (isAutoClose()) {
            close(session);
        }
    }
}
Also used : KeycloakTransactionManager(org.keycloak.models.KeycloakTransactionManager) KeycloakSession(org.keycloak.models.KeycloakSession) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory)

Example 3 with KeycloakTransactionManager

use of org.keycloak.models.KeycloakTransactionManager in project keycloak by keycloak.

the class QuarkusKeycloakApplication method createAdminUser.

private void createAdminUser() {
    String adminUserName = System.getenv(KEYCLOAK_ADMIN_ENV_VAR);
    String adminPassword = System.getenv(KEYCLOAK_ADMIN_PASSWORD_ENV_VAR);
    if ((adminUserName == null || adminUserName.trim().length() == 0) || (adminPassword == null || adminPassword.trim().length() == 0)) {
        return;
    }
    KeycloakSessionFactory sessionFactory = KeycloakApplication.getSessionFactory();
    KeycloakSession session = sessionFactory.create();
    KeycloakTransactionManager transaction = session.getTransactionManager();
    try {
        transaction.begin();
        new ApplianceBootstrap(session).createMasterRealmUser(adminUserName, adminPassword);
        ServicesLogger.LOGGER.addUserSuccess(adminUserName, Config.getAdminRealm());
        transaction.commit();
    } catch (IllegalStateException e) {
        session.getTransactionManager().rollback();
        ServicesLogger.LOGGER.addUserFailedUserExists(adminUserName, Config.getAdminRealm());
    } catch (Throwable t) {
        session.getTransactionManager().rollback();
        ServicesLogger.LOGGER.addUserFailed(t, adminUserName, Config.getAdminRealm());
    } finally {
        session.close();
    }
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakTransactionManager(org.keycloak.models.KeycloakTransactionManager) KeycloakSession(org.keycloak.models.KeycloakSession) QuarkusKeycloakSessionFactory(org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory)

Aggregations

KeycloakTransactionManager (org.keycloak.models.KeycloakTransactionManager)3 KeycloakSession (org.keycloak.models.KeycloakSession)2 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)2 QuarkusKeycloakSessionFactory (org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory)1 ApplianceBootstrap (org.keycloak.services.managers.ApplianceBootstrap)1