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();
}
}
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;
}
}
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();
}
}
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;
}
Aggregations