fix: Keep MSession/MRole cache in sync within each JVM#600
Merged
yamelsenih merged 1 commit intoJun 2, 2026
Merged
Conversation
When two JVMs share the same database (e.g. `adempiere-grpc-server` + `adempiere-report-engine-service`), their in-memory caches diverge after writes performed by one of them: the second JVM keeps serving the previous state until TTL. This is most visible after `change-role` — reports run with the wrong tenant/org filter and return empty. ## Changes - `MSession.afterSave(...)`: new override that refreshes `s_sessions` with the current instance on every successful save. Keeps the cache aligned with the row's actual state (including `Processed`) without an extra DB hit on read. Processed sessions stay visible — callers that need to reject them (e.g. `KeycloakSessionHandler.loadExistingSessionContext`) check `isProcessed()` themselves; callers that need them (audit log, changelog, keepalive on closing sessions) still get the row. - `MSession.logout()`: dropped the redundant explicit `s_sessions.remove(...)` — `afterSave` already refreshes the entry after `setProcessed(true)` + `saveEx()`. - `MRole.removeFromCache(int roleId, int userId)`: new static helper to evict a `(role, user)` cache entry so the next `get()` rebuilds the access SQL. Required when the user's effective tenant/org changes. ## Test Login as System → change role to a company → run a report whose query goes through `MRole.addAccessSQL`. SQL filter must contain the company's `AD_Client_ID` / `AD_Org_ID`, not `0, 0`. Full cross-JVM coverage still requires a distributed cache (out of scope here).
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.
When two JVMs share the same database (e.g.
adempiere-grpc-server+adempiere-report-engine-service), their in-memory caches diverge after writes performed by one of them: the second JVM keeps serving the previous state until TTL. This is most visible afterchange-role— reports run with the wrong tenant/org filter and return empty.Changes
MSession.afterSave(...): new override that refreshess_sessionswith the current instance on every successful save. Keeps the cache aligned with the row's actual state (includingProcessed) without an extra DB hit on read. Processed sessions stay visible — callers that need to reject them (e.g.KeycloakSessionHandler.loadExistingSessionContext) checkisProcessed()themselves; callers that need them (audit log, changelog, keepalive on closing sessions) still get the row.MSession.logout(): dropped the redundant explicits_sessions.remove(...)—afterSavealready refreshes the entry aftersetProcessed(true)+saveEx().MRole.removeFromCache(int roleId, int userId): new static helper to evict a(role, user)cache entry so the nextget()rebuilds the access SQL. Required when the user's effective tenant/org changes.Test
Login as System → change role to a company → run a report whose query goes through
MRole.addAccessSQL. SQL filter must contain the company'sAD_Client_ID/AD_Org_ID, not0, 0. Full cross-JVM coverage still requires a distributed cache (out of scope here).