diff --git a/services/src/main/java/org/keycloak/services/filters/KeycloakTransactionCommitter.java b/services/src/main/java/org/keycloak/services/filters/KeycloakTransactionCommitter.java new file mode 100644 index 0000000000..ab3bc1bcbb --- /dev/null +++ b/services/src/main/java/org/keycloak/services/filters/KeycloakTransactionCommitter.java @@ -0,0 +1,28 @@ +/* + */ + +package org.keycloak.services.filters; + +import org.jboss.resteasy.spi.ResteasyProviderFactory; +import org.keycloak.models.KeycloakTransaction; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import java.io.IOException; + +/** + * @author Stian Thorgersen + */ +public class KeycloakTransactionCommitter implements ContainerResponseFilter { + + @Override + public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException { + KeycloakTransaction tx = ResteasyProviderFactory.getContextData(KeycloakTransaction.class); + if (tx != null && tx.isActive()) { + if (tx.getRollbackOnly()) tx.rollback(); + else tx.commit(); + } + } + +} diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java index 136f634d07..7b261f24ae 100755 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -15,6 +15,7 @@ import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.services.DefaultKeycloakSessionFactory; +import org.keycloak.services.filters.KeycloakTransactionCommitter; import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.BruteForceProtector; import org.keycloak.services.managers.RealmManager; @@ -75,6 +76,8 @@ public class KeycloakApplication extends Application { classes.add(ThemeResource.class); classes.add(JsResource.class); + classes.add(KeycloakTransactionCommitter.class); + singletons.add(new ObjectMapperResolver(Boolean.parseBoolean(System.getProperty("keycloak.jsonPrettyPrint", "false")))); migrateModel();