Search in sources :

Example 1 with KeycloakTransaction

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

the class KeycloakModelUtils method runJobInTransaction.

/**
 * Wrap given runnable job into KeycloakTransaction.
 *
 * @param factory
 * @param task
 */
public static void runJobInTransaction(KeycloakSessionFactory factory, KeycloakSessionTask task) {
    KeycloakSession session = factory.create();
    KeycloakTransaction tx = session.getTransactionManager();
    try {
        tx.begin();
        task.run(session);
        if (tx.isActive()) {
            if (tx.getRollbackOnly()) {
                tx.rollback();
            } else {
                tx.commit();
            }
        }
    } catch (RuntimeException re) {
        if (tx.isActive()) {
            tx.rollback();
        }
        throw re;
    } finally {
        session.close();
    }
}
Also used : KeycloakSession(org.keycloak.models.KeycloakSession) KeycloakTransaction(org.keycloak.models.KeycloakTransaction)

Example 2 with KeycloakTransaction

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

the class DefaultKeycloakTransactionManager method commit.

@Override
public void commit() {
    if (completed) {
        return;
    } else {
        completed = true;
    }
    RuntimeException exception = null;
    for (KeycloakTransaction tx : prepare) {
        try {
            tx.commit();
        } catch (RuntimeException e) {
            exception = exception == null ? e : exception;
        }
    }
    if (exception != null) {
        rollback(exception);
        return;
    }
    for (KeycloakTransaction tx : transactions) {
        try {
            tx.commit();
        } catch (RuntimeException e) {
            exception = exception == null ? e : exception;
        }
    }
    // Don't commit "afterCompletion" if commit of some main transaction failed
    if (exception == null) {
        for (KeycloakTransaction tx : afterCompletion) {
            try {
                tx.commit();
            } catch (RuntimeException e) {
                exception = exception == null ? e : exception;
            }
        }
    } else {
        for (KeycloakTransaction tx : afterCompletion) {
            try {
                tx.rollback();
            } catch (RuntimeException e) {
                ServicesLogger.LOGGER.exceptionDuringRollback(e);
            }
        }
    }
    active = false;
    if (exception != null) {
        throw exception;
    }
}
Also used : KeycloakTransaction(org.keycloak.models.KeycloakTransaction)

Example 3 with KeycloakTransaction

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

the class KeycloakErrorHandler method toResponse.

@Override
public Response toResponse(Throwable throwable) {
    KeycloakSession session = Resteasy.getContextData(KeycloakSession.class);
    KeycloakTransaction tx = session.getTransactionManager();
    tx.setRollbackOnly();
    int statusCode = getStatusCode(throwable);
    if (statusCode >= 500 && statusCode <= 599) {
        logger.error(UNCAUGHT_SERVER_ERROR_TEXT, throwable);
    } else {
        logger.debugv(throwable, ERROR_RESPONSE_TEXT, statusCode);
    }
    if (!MediaTypeMatcher.isHtmlRequest(headers)) {
        OAuth2ErrorRepresentation error = new OAuth2ErrorRepresentation();
        error.setError(getErrorCode(throwable));
        return Response.status(statusCode).header(HttpHeaders.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE.toString()).entity(error).build();
    }
    try {
        RealmModel realm = resolveRealm(session);
        Theme theme = session.theme().getTheme(Theme.Type.LOGIN);
        Locale locale = session.getContext().resolveLocale(null);
        FreeMarkerUtil freeMarker = new FreeMarkerUtil();
        Map<String, Object> attributes = initAttributes(session, realm, theme, locale, statusCode);
        String templateName = "error.ftl";
        String content = freeMarker.processTemplate(attributes, templateName, theme);
        return Response.status(statusCode).type(MediaType.TEXT_HTML_UTF_8_TYPE).entity(content).build();
    } catch (Throwable t) {
        logger.error("Failed to create error page", t);
        return Response.serverError().build();
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) Locale(java.util.Locale) FreeMarkerUtil(org.keycloak.theme.FreeMarkerUtil) KeycloakSession(org.keycloak.models.KeycloakSession) OAuth2ErrorRepresentation(org.keycloak.representations.idm.OAuth2ErrorRepresentation) Theme(org.keycloak.theme.Theme) KeycloakTransaction(org.keycloak.models.KeycloakTransaction)

Example 4 with KeycloakTransaction

use of org.keycloak.models.KeycloakTransaction 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

KeycloakTransaction (org.keycloak.models.KeycloakTransaction)4 KeycloakSession (org.keycloak.models.KeycloakSession)2 Locale (java.util.Locale)1 TransactionManager (javax.transaction.TransactionManager)1 KeycloakTransactionManager (org.keycloak.models.KeycloakTransactionManager)1 RealmModel (org.keycloak.models.RealmModel)1 OAuth2ErrorRepresentation (org.keycloak.representations.idm.OAuth2ErrorRepresentation)1 FreeMarkerUtil (org.keycloak.theme.FreeMarkerUtil)1 Theme (org.keycloak.theme.Theme)1 JtaTransactionManagerLookup (org.keycloak.transaction.JtaTransactionManagerLookup)1 JtaTransactionWrapper (org.keycloak.transaction.JtaTransactionWrapper)1