Repository navigation
Load the organizations managing a user once per session - #55775
Open
ivayloatns87 wants to merge 1 commit into
Open
ivayloatns87 wants to merge 1 commit into
ivayloatns87 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new test would also pass with the original per-organization queries, leaving the optimization unprotected without query-count assertions and reload verification after membership changes.
1 open issue
What changed in this PR
Reduces repeated database queries during organization membership checks by caching managing-organization IDs within each Keycloak session.
Changes:
- Adds a realm-scoped query for managing-organization IDs.
- Reuses results and invalidates them after membership changes and locking.
- Adds integration coverage for membership updates, login, and removal.
| File | Description |
|---|---|
| tests/base/src/test/java/org/keycloak/tests/organization/member/OrganizationMemberTest.java | Tests managed membership among multiple organizations. |
| model/jpa/src/main/java/org/keycloak/organization/jpa/JpaOrganizationProvider.java | Adds session-scoped lookup caching and invalidation. |
| model/jpa/src/main/java/org/keycloak/models/jpa/entities/OrganizationEntity.java | Defines the managing-organization ID query. |
Review effort: Balanced
Closes keycloak#55257 Signed-off-by: Ivaylo Atanasov <ivayloatns87@gmail.com>
ivayloatns87
force-pushed
the
issue-55257-managed-member-lookup
branch
from
December 3, 2026 09:16
7c1b54b to
6df3036
Compare
Author
|
@martin-kanis this implements the per-session approach you suggested in #55257. The workflow runs are waiting for approval whenever you have a moment. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Closes #55257
JpaOrganizationProvider.isManagedMembernow answers from the ids of the organizations that manage the user, loaded with one query per session, soOrganizations.resolveOrganizationandOrganizations.isReadOnlyOrganizationMemberno longer run two queries for every organization of the user.Decisions and assumptions:
KeycloakSessionattribute keyed by realm and user id, following existing session-scoped lookups such as theauthSession.user.<id>attribute. No new cache or provider API is added, and the organization-selection order is unchanged.addMember,addManagedMember,updateMembershipType,removeMember, and therefore organization removal), before the join and leave events fire.throwIfManagedByAnotherOrgclears the attribute after taking the pessimistic lock on the user, so the check still reads memberships committed by a concurrent request instead of a set loaded earlier in the same request.falsewithout a query. An organization that does not exist or belongs to another realm now returnsfalseinstead of failing ingetOrganizationGroup, since validating it would bring back one query per organization.I used an AI coding agent (Claude Code) to write this change and its test; I reviewed and tested it.