Repository navigation
Simplifying session context handling, ensuring an always started transaction #48471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
61d615b
b83c4e8
c247936
1aebc93
dfa313e
e960045
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import jakarta.inject.Inject; | ||
| import jakarta.ws.rs.WebApplicationException; | ||
| import jakarta.ws.rs.core.HttpHeaders; | ||
| import jakarta.ws.rs.core.Response; | ||
|
|
@@ -19,14 +20,12 @@ | |
| import org.keycloak.forms.login.MessageType; | ||
| import org.keycloak.forms.login.freemarker.model.UrlBean; | ||
| import org.keycloak.models.KeycloakSession; | ||
| import org.keycloak.models.KeycloakSessionTaskWithResult; | ||
| import org.keycloak.models.KeycloakTransaction; | ||
| import org.keycloak.models.ModelDuplicateException; | ||
| import org.keycloak.models.ModelException; | ||
| import org.keycloak.models.ModelIllegalStateException; | ||
| import org.keycloak.models.ModelValidationException; | ||
| import org.keycloak.models.RealmModel; | ||
| import org.keycloak.models.utils.KeycloakModelUtils; | ||
| import org.keycloak.representations.idm.OAuth2ErrorRepresentation; | ||
| import org.keycloak.services.managers.RealmManager; | ||
| import org.keycloak.services.messages.Messages; | ||
|
|
@@ -36,16 +35,13 @@ | |
| import org.keycloak.theme.beans.MessageBean; | ||
| import org.keycloak.theme.beans.MessageFormatterMethod; | ||
| import org.keycloak.theme.freemarker.FreeMarkerProvider; | ||
| import org.keycloak.utils.KeycloakSessionUtil; | ||
| import org.keycloak.utils.MediaType; | ||
| import org.keycloak.utils.MediaTypeMatcher; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParseException; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import org.jboss.logging.Logger; | ||
|
|
||
| import static org.keycloak.services.resources.KeycloakApplication.getSessionFactory; | ||
|
|
||
| @Provider | ||
| public class KeycloakErrorHandler implements ExceptionMapper<Throwable> { | ||
|
|
||
|
|
@@ -56,21 +52,11 @@ public class KeycloakErrorHandler implements ExceptionMapper<Throwable> { | |
| public static final String UNCAUGHT_SERVER_ERROR_TEXT = "Uncaught server error"; | ||
| public static final String ERROR_RESPONSE_TEXT = "Error response {0}"; | ||
|
|
||
| @Inject | ||
| KeycloakSession session; | ||
|
|
||
| @Override | ||
| public Response toResponse(Throwable throwable) { | ||
| KeycloakSession session = KeycloakSessionUtil.getKeycloakSession(); | ||
|
|
||
| if (session == null) { | ||
| // errors might be thrown when handling errors from JAX-RS before the session is available | ||
| return KeycloakModelUtils.runJobInTransactionWithResult(getSessionFactory(), | ||
| new KeycloakSessionTaskWithResult<Response>() { | ||
| @Override | ||
| public Response run(KeycloakSession session) { | ||
| return getResponse(session, throwable); | ||
| } | ||
| }); | ||
| } | ||
|
Comment on lines
-63
to
-72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that this change causes that running tests from IDE results in a NPE? I try to run The suppressed exception is (though this seems normal, as the test appears to try a cleanup at the beginning and handles the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vilmosnagy yes these changes caused that to happen. See #48838 cc @ahus1 |
||
|
|
||
| return getResponse(session, throwable); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall starting the transaction when the session was not assoicated with the thread because that should only be done once for the lifecylce. My concern with isActive is that it will return false after the transaction is complete, so we could be starting multiple transactions when not intended for the nested calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shawkins - I don't think starting a transaction a second time will happen as the transaction is supposed to be closed at the end of the request, not earlier. And once it is bound to a thread, it will not leave the thread.
I've updated Keycloak's default transaction to ensure that a transaction is not started or augmented after it has completed. The tests are green, I only needed to update two tests that were obviously wrong.
The alternative would be to add another method to the
KeycloakTransactionManager, but I think this way is just safer. You're about to do something very wrong when you close the transaction earlier, or try to start it multiple times.Let me know your thoughts. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had responded here #48471 (comment) without seeing this initially.
I'm good with the change to the transaction manager.
Given that refinement using isActive here is good - should a developer do something that completes the session transaction early, we'll get an exception here.
The only other thought is that in the past @pedroigor wanted the methods of TransactionalSessionHandler to be used, rather than direct calls to transactionManager.begin();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall discussing keeping the
TransactionalSessionHandlerinterface so that we easily identify the components involved in the lifecycle of "transactional (keycloak) sessions".